Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Create Schema Error

Author  Topic 

pravin14u
Posting Yak Master

246 Posts

Posted - 2008-03-25 : 01:52:05
IF NOT EXISTS (SELECT 1 FROM Sys.Schemas WHERE [Name] = N'HR4')
CREATE SCHEMA HR4 AUTHORIZATION [dbo]


The above statements gives me an error saying "Incorrect syntax near schema". But the following code works fine.


DECLARE @sql varchar(100)
set @sql='CREATE SCHEMA HR4 AUTHORIZATION [dbo]'
IF NOT EXISTS (SELECT 1 FROM Sys.Schemas WHERE [Name] = N'HR4')
exec(@sql)


Any ideas on what is causing this error?

Thanks!

SusanthaB
Starting Member

14 Posts

Posted - 2008-03-25 : 06:27:19
CREATE SCHEMA should be the only statement of the batch. If you seperate the two statements with GO, it will work fine but your requirement will not statisfy.

The only solution would be to use dynamic sql as you have given in the 2nd choice.

Susantha Bathige
Senior DBA, Sri Lanka
Go to Top of Page
   

- Advertisement -