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.
| Author |
Topic |
|
mmalaka
Starting Member
33 Posts |
Posted - 2008-06-19 : 11:01:47
|
Experts;I am use the Query analyser to buidl a Master Stored Procedure that would go through the Stored Procedures in the DB to recreate them againI am tesing this in the Query AnalyserThis is the section of my code to create on of the Stored Procedures in the DataBaseselect @SQL = 'if exists (select * from dbo.sysobjects where id = object_id(N''[dbo].[IMGSAttributes'+@TableName+']'') and OBJECTPROPERTY(id, N''IsProcedure'') = 1)'select @SQL = @SQL + char(13) + char(10) + 'drop procedure [dbo].[IMGSAttributes'+@TableName+']'select @SQL = @SQL + char(13) + char(10) + 'GO'+ char(13) + char(10) +'SET QUOTED_IDENTIFIER ON'+ char(13) + char(10) +'GO'+ char(13) + char(10) +'SET ANSI_NULLS ON'+ char(13) + char(10) +'GO'+ char(13) + char(10) +'CREATE PROCEDURE dbo.IMGSAttributes'+@TableNameselect @SQL = @SQL + char(13) + char(10) + '@pIndex varchar(50)'+ char(13) + char(10) + 'AS'+ char(13) + char(10) +'SET CONCAT_NULL_YIELDS_NULL OFF'+ char(13) + char(10) +'Select '+@PrimaryColumName + ' as GMKey,'+''''+@TableName +''' as TABLENAME, '+@ColTextselect @SQL = @SQL + ' from '+@TableName+' where ' +@PrimaryColumName +' = @pIndex'select @SQL = @SQL + char(13) + char(10) + 'RETURN '+ char(13) + char(10) +'GO '+ char(13) + char(10) +' SET QUOTED_IDENTIFIER OFF '+ char(13) + char(10) +'GO'+ char(13) + char(10) +'SET ANSI_NULLS ON '+ char(13) + char(10) +'GO'exec(@SQL) When I run this I got some errors:Server: Msg 170, Level 15, State 1, Line 3Line 3: Incorrect syntax near 'GO'.Server: Msg 170, Level 15, State 1, Line 5Line 5: Incorrect syntax near 'GO'.Server: Msg 170, Level 15, State 1, Line 7Line 7: Incorrect syntax near 'GO'.Server: Msg 111, Level 15, State 1, Line 8'CREATE PROCEDURE' must be the first statement in a query batch.Server: Msg 137, Level 15, State 1, Line 12Must declare the variable '@pIndex'.Server: Msg 178, Level 15, State 1, Line 15A RETURN statement with a return value cannot be used in this context.Server: Msg 170, Level 15, State 1, Line 16Line 16: Incorrect syntax near 'GO'.Server: Msg 170, Level 15, State 1, Line 18Line 18: Incorrect syntax near 'GO'. |
|
|
chiragkhabaria
Master Smack Fu Yak Hacker
1907 Posts |
Posted - 2008-06-19 : 11:04:55
|
| You wont be able to use GO , while running using Dynamic sql.Chiraghttp://www.chirikworld.com |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-06-19 : 11:06:34
|
| Replace the Exec(@Sql) with Print(@sql) and see what string its printing out. |
 |
|
|
mmalaka
Starting Member
33 Posts |
Posted - 2008-06-19 : 11:19:06
|
quote: Originally posted by visakh16 Replace the Exec(@Sql) with Print(@sql) and see what string its printing out.
it is printing this:if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[IMGSAttributesCFM_CLASS_SWIMMING_POOLS]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)drop procedure [dbo].[IMGSAttributesCFM_CLASS_SWIMMING_POOLS]GOSET QUOTED_IDENTIFIER ONGOSET ANSI_NULLS ONGOCREATE PROCEDURE dbo.IMGSAttributesCFM_CLASS_SWIMMING_POOLS@pIndex varchar(50)ASSET CONCAT_NULL_YIELDS_NULL OFFSelect PRIMARYINDEX as GMKey,'CFM_CLASS_SWIMMING_POOLS' as TABLENAME, '<b>Facility Name</b><br/>' + cast(Facility_Name as Char) + '<br/>' +'<b>Building No</b><br/>' + cast(Building_Number as Char) + '<br/>' +'<b>Street</b><br/>' + cast(Street_Name as Char) + '<br/>' +'<b>District</b><br/>' + cast(Postal_District as Char) + '<br/>' +'<b>Committee Area</b><br/>' + cast(Committee_Area as Char) + '<br/>' +'<b>Sub Class</b><br/>' + cast(SubClass as Char) + '<br/>' +'<b>Phone No</b><br/>' + cast(Phone_No as Char) + '<br/>' +'<b>URL</b><br/>' + cast(Url as Char) + '<br/>' as Details from CFM_CLASS_SWIMMING_POOLS where PRIMARYINDEX = @pIndexRETURN GO SET QUOTED_IDENTIFIER OFF GOSET ANSI_NULLS ON GOWhen I run this on a new Query it work fine |
 |
|
|
|
|
|
|
|