Posted by Erland Sommarskog on 08/16/07 10:50
(gancy26@gmail.com) writes:
> SET @DBName = 'Archive_SafeHelp'
> CREATE DATABASE @DBName + ' ' + @From + ' ' + @To
> END
>
> I am trying to create a database based on the name contained in the
> variables. I get the error 'Incorrect syntax near '@DBName'. How do
> i accomplish this?
You need to use dynamic SQL:
SELECT @sql = 'CREATE DATABASE ' + @DBName + ' ' + @From + ' ' + @To
EXEC sp_executesql @sql
For more information on dynamic SQL, I have an article on my web site:
http://www.sommarskog.se/dynamic_sql.html
--
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se
Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx
Navigation:
[Reply to this message]
|