March 18, 2010 at 7:57 am
Hi,
I am using this strategy to deploy the same stored procedure to several databases:
http://www.kodyaz.com/articles/Create-Stored-Procedure-using-sp_msforeachdb-on-all-databases.aspx
However, I am getting an Incorrect syntax near 'False' error
Here is a sample sp that I am deploying:
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go
CREATE PROC Test1
@StartDate DateTime = NULL,
@EndDate DateTime = NULL
AS
BEGIN
SET NOCOUNT ON
DECLARE @Id as Int
select * from dbo.MyTable
where IsTrue = 'False'
and Date between @StartDate and @EndDate
and Id = @Id
RETURN 0
END
Here is what I am running:
EXEC sp_MSForEachDB 'CreateProcedure ''[?]'', ''Test1'''
I really appreciate any help.
March 18, 2010 at 8:25 am
It's probably a question of string building. False has single-quotes around it that are probably breaking the dynamic SQL.
Try replacing that with a 0 (assuming it's a bit field), and see if that works.
- Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
Property of The Thread
"Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon
March 18, 2010 at 8:30 am
I alredy tried that, but I get the same error on each area that I have a string. My real sp has a lot of strings. I cannot just change them all to 0 or 1.
March 18, 2010 at 8:34 am
Then you'll need to use a different deployment method.
I would generally not include the same proc in multiple databases. I have a database called "Common" where I store that kind of thing. Set up the proc to have a parameter for the database name, and then use either Union commands or dynamic SQL to query just the one table you want. That way you only need to maintain the code in one place.
- Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
Property of The Thread
"Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon
March 18, 2010 at 8:39 am
I must deploy the same sp to several databases.
March 18, 2010 at 8:46 am
Write a dynamic SQL script that will allow you to do so. Here's a simple example:
DECLARE @SQL NVARCHAR(MAX);
SELECT @SQL = 'CREATE PROC Test1
@StartDate DateTime = NULL,
@EndDate DateTime = NULL
AS
BEGIN
SET NOCOUNT ON
DECLARE @Id as Int
select * from dbo.MyTable
where IsTrue = ''False''
and Date between @StartDate and @EndDate
and Id = @Id
RETURN 0';
SELECT 'use ' + NAME + '; exec (''' + @SQL + ''');'
FROM sys.databases AS D;
Copy-and-paste the results into a new query window and then run them. (Verify they are correct first, of course.)
Or make a cursor to step over each row of that, and execute them automatically from that.
Or concatenate that into a single script variable and execute that.
Or get a copy of RedGate's multi-script product and make it do the work for you.
There are lots of options.
- Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
Property of The Thread
"Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon
March 18, 2010 at 9:48 am
Thank you very much for the idea. I tried it with copy and paste the results, but I received the same error: Incorrect Syntax near 'False', and I also received additional errors: must declare scalar variable @bla
March 19, 2010 at 5:59 am
I'd have to see the resulting script to tell you what's wrong with it.
- Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
Property of The Thread
"Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon
March 26, 2010 at 6:07 pm
Gus, I'm suspecting that [IsTrue] is actually a bit column and not a string.
[font="Times New Roman"]-- RBarryYoung[/font], [font="Times New Roman"] (302)375-0451[/font] blog: MovingSQL.com, Twitter: @RBarryYoung[font="Arial Black"]
Proactive Performance Solutions, Inc. [/font][font="Verdana"] "Performance is our middle name."[/font]
Viewing 9 posts - 1 through 9 (of 9 total)
You must be logged in to reply to this topic. Login to reply