Click here to Skip to main content
15,891,431 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I am using dynamic sql to create a new schema and to create a table in that new created schem. I have written following stored procedure.
SQL
IF exists(select * from sys.objecs where name=dbo.BuildClientSchema and [schema_id]=SCHEMA_ID('dbo'))
drop procedure dbo.BuildClientSchema
go

CREATE PROCEDURE dbo.BuildClientSchema
@schemaname NVARCHAR(20)
AS
BEGIN
DECLARE @sql NVARCHAR(MAX) = N'CREATE SCHEMA [' + @schemaName + '] AUTHORIZATION [dbo];'

PRINT @sql

EXEC @sql

SET @sql = 'CREATE TABLE [' + @schemaName + '].[Products](' + CHAR(13) + CHAR(10) +
        CHAR(9) + 'Id INT PRIMARY KEY,' + CHAR(13) + CHAR(10) +
        CHAR(9) + 'Name NVARCHAR(30) NOT NULL,' + CHAR(13) + CHAR(10) +
        CHAR(9) + 'ProductDesc NVARCHAR(100),' + CHAR(13) + CHAR(10) +
        CHAR(9) + 'Price DECIMAL (20),' + CHAR(13) + CHAR(10) +
        CHAR(9) + 'CategoryId INT NOT NULL)'

		PRINT @sql

		EXEC @sql

END

It shows me error in if statement that the statement is not recognized - error - SQL70001. Help to resolve this.
Posted
Comments
Om Prakash Pant 18-Jun-15 0:21am    
I think in this statement,"dbo.BuildClientSchema" should be in single quotes as well.
select * from sys.objecs where name=dbo.BuildClientSchema ....
Dolly Nimavat 18-Jun-15 0:24am    
I did it. Still the same error.
Dolly Nimavat 18-Jun-15 0:26am    
I also found the answer that change build action property of the stored procedure. But in properties window there is no such property exists.
Abhipal Singh 18-Jun-15 11:53am    
Build action property comes under visual studio .dbproj projects. Basically, it is used to specify if the sql file is to be considered for build or not.

Ignore that for now, if you are not using visual studio's database projects.

If you are using Visual studio projects then you need to omit the IF statement as it will not work there.
.dbproj uses a different strategy to achieve what you are trying to do using IF clause.
Read the link below to see how you can use database project settings:
https://msdn.microsoft.com/en-us/library/aa833291(v=vs.100).aspx

Also, there is an issue with you IF statement. please replace it with the statement below:
IF exists(select * from sys.objects where name='dbo.BuildClientSchema' and [schema_id]=SCHEMA_ID('dbo'))
drop procedure dbo.BuildClientSchema
go
Dolly Nimavat 18-Jun-15 12:34pm    
I am not using database project.So now what can I do instead of using if statement?

Try this:
SQL
IF exists(select * from sys.objects where name='dbo.BuildClientSchema' and [schema_id]=SCHEMA_ID('dbo'))
drop procedure dbo.BuildClientSchema;
go


Your code had 1) sys.objecs instead of sys.objects 2) No quotes around procedure name
 
Share this answer
 
Comments
Dolly Nimavat 18-Jun-15 12:31pm    
I tried it. Still the same error.
I believe I gave you this SQL the other day.

First, in your IF EXISTS statement, you need to place the object in single quotes. You also can't put the schema name in it has well. If you query the sys.objects view, you will see why. Your schema is filtered on the [schema_id]=SCHEMA_ID('dbo') part of the condition.

SQL
IF EXISTS(SELECT * FROM sys.objecs WHERE name='BuildClientSchema' AND [schema_id]=SCHEMA_ID('dbo'))
    DROP PROCEDURE dbo.BuildClientSchema
GO


If this statement is still giving you trouble, try this:

SQL
IF EXISTS(SELECT * FROM sys.objecs WHERE name='BuildClientSchema' AND [schema_id]=SCHEMA_ID('dbo'))
BEGIN
    DROP PROCEDURE dbo.BuildClientSchema
END
GO


Now, the IF EXISTS statement is not apart of your stored procedure definition. So if you are adding this to a SQL Server Database project, This line needs to be removed. The Visual Studio SSDT will actually handle when to do a CREATE vs an ALTER when deploying a database.

Also, if you are using a database project or SSDT, you will need to add reference to the master database so that build tools knows where sp_executesql lives on the server. This link shows how to do that:
SSDT: unresolved reference to object [dbo].[sp_executesql].[^]

If you are getting an error trying to execute the stored procedure, it is because you are using EXE @sql instead of EXE sp_executesql @sql. To understand this better, take a look at this article to better understand dynamic SQL:
Execute Dynamic SQL commands in SQL Server[^]
 
Share this answer
 
v3
Comments
Dolly Nimavat 18-Jun-15 12:29pm    
Writing the statement - EXEC sp_executesql @sql - gives the error - 'the procedure has an unresolved reference to object [dbo].[sp_executesql]'
virusstorm 18-Jun-15 13:01pm    
Are you doing this inside a SQL Server Database Project? If so, what version of Visual Studio are you using?
Dolly Nimavat 18-Jun-15 13:09pm    
Yes. I am using VS2013 and sqlserver 2012
virusstorm 18-Jun-15 13:14pm    
This will do the trick for you:
http://sqlserverselect.blogspot.com/2012/08/ssdt-unresolved-reference-to-object.html
Dolly Nimavat 18-Jun-15 13:25pm    
But i can't understand why there is problem with if statement?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900