You are here: Re: Creating database from stored proc with variable holding the database name « MsSQL Server « IT news, forums, messages
Re: Creating database from stored proc with variable holding the database name

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]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация