all groups > sql server programming > may 2007 >
You're in the sql server programming group:
Dynamic Query Syntax
sql server programming:
This sproc is called from Vb.NET code with different outputTable and countyname values. Keep getting error: Must declare the scalar variable "@countyname". ALTER PROCEDURE [dbo].[_spInsert_output_all2] (@outputTable nvarchar(32), @countyname varchar(25)) AS DECLARE @SQL Nvarchar(1000) SET @SQL = 'INSERT INTO dbName.dbo.output_' + @outputTable + ' ( countyname ) VALUES ( @countyname)' EXEC sp_executesql @SQL, N'@countynameIN nvarchar(25)', @countynameIN = @countyname
You are using @countyname instead of @countynameIN inside your sql string. -- Sylvain Lafontaine, ing. MVP - Technologies Virtual-PC E-mail: sylvain aei ca (fill the blanks, no spam please) [quoted text, click to view] "Bil Click" <BilClick@discussions.microsoft.com> wrote in message news:685B7372-B042-4FE5-B114-D1671EDC8686@microsoft.com... > This sproc is called from Vb.NET code with different outputTable and > countyname values. Keep getting error: Must declare the scalar variable > "@countyname". > > ALTER PROCEDURE [dbo].[_spInsert_output_all2] > (@outputTable nvarchar(32), > @countyname varchar(25)) > AS > DECLARE @SQL Nvarchar(1000) > SET @SQL = > 'INSERT INTO dbName.dbo.output_' + @outputTable + ' ( countyname ) > VALUES ( @countyname)' > > EXEC sp_executesql @SQL, > N'@countynameIN nvarchar(25)', > @countynameIN = @countyname >
Thank you Sylvain. If I use this, I get syntax error near '=' ALTER PROCEDURE [dbo].[_spInsert_output_all2] (@outputTable nvarchar(32), @countyname varchar(25)) AS DECLARE @SQL Nvarchar(1000) SET @SQL = 'INSERT INTO dbName.dbo.output_' + @outputTable + ' ( countyname ) VALUES ( @countyname)' EXEC sp_executesql @SQL, N'@countyname nvarchar(25)', countyname = @countyname [quoted text, click to view] "Sylvain Lafontaine" wrote: > You are using @countyname instead of @countynameIN inside your sql string. > > -- > Sylvain Lafontaine, ing. > MVP - Technologies Virtual-PC > E-mail: sylvain aei ca (fill the blanks, no spam please) > > > "Bil Click" <BilClick@discussions.microsoft.com> wrote in message > news:685B7372-B042-4FE5-B114-D1671EDC8686@microsoft.com... > > This sproc is called from Vb.NET code with different outputTable and > > countyname values. Keep getting error: Must declare the scalar variable > > "@countyname". > > > > ALTER PROCEDURE [dbo].[_spInsert_output_all2] > > (@outputTable nvarchar(32), > > @countyname varchar(25)) > > AS > > DECLARE @SQL Nvarchar(1000) > > SET @SQL = > > 'INSERT INTO dbName.dbo.output_' + @outputTable + ' ( countyname ) > > VALUES ( @countyname)' > > > > EXEC sp_executesql @SQL, > > N'@countynameIN nvarchar(25)', > > @countynameIN = @countyname > > > >
Thanks to all of you, but I still cannot get complete syntax correct. My vb.Net code repeatedly calls this sproc using different tables and countyname. That is why I was using sp_executesql and dynamic sql. I am still confused on how & where to declare & set the variables. If anyone could show the complete script I would greatly appreciate it!!! [quoted text, click to view] "Sylvain Lafontaine" wrote: > I mean in the following place: > > SET @SQL = > 'INSERT INTO dbName.dbo.output_' + @outputTable + ' ( countyname ) > VALUES ( @countynameIN)' > > instead of: > > SET @SQL = > 'INSERT INTO dbName.dbo.output_' + @outputTable + ' ( countyname ) > VALUES ( @countyname)' > > -- > Sylvain Lafontaine, ing. > MVP - Technologies Virtual-PC > E-mail: sylvain aei ca (fill the blanks, no spam please) > > > "Bil Click" <BilClick@discussions.microsoft.com> wrote in message > news:CF56C79A-A350-4A2F-B283-F2C39A65F517@microsoft.com... > > Thank you Sylvain. If I use this, I get syntax error near '=' > > > > ALTER PROCEDURE [dbo].[_spInsert_output_all2] > > (@outputTable nvarchar(32), > > @countyname varchar(25)) > > AS > > DECLARE @SQL Nvarchar(1000) > > SET @SQL = > > 'INSERT INTO dbName.dbo.output_' + @outputTable + ' ( countyname ) > > VALUES ( @countyname)' > > > > EXEC sp_executesql @SQL, > > N'@countyname nvarchar(25)', > > countyname = @countyname > > > > > > > > "Sylvain Lafontaine" wrote: > > > >> You are using @countyname instead of @countynameIN inside your sql > >> string. > >> > >> -- > >> Sylvain Lafontaine, ing. > >> MVP - Technologies Virtual-PC > >> E-mail: sylvain aei ca (fill the blanks, no spam please) > >> > >> > >> "Bil Click" <BilClick@discussions.microsoft.com> wrote in message > >> news:685B7372-B042-4FE5-B114-D1671EDC8686@microsoft.com... > >> > This sproc is called from Vb.NET code with different outputTable and > >> > countyname values. Keep getting error: Must declare the scalar variable > >> > "@countyname". > >> > > >> > ALTER PROCEDURE [dbo].[_spInsert_output_all2] > >> > (@outputTable nvarchar(32), > >> > @countyname varchar(25)) > >> > AS > >> > DECLARE @SQL Nvarchar(1000) > >> > SET @SQL = > >> > 'INSERT INTO dbName.dbo.output_' + @outputTable + ' ( countyname ) > >> > VALUES ( @countyname)' > >> > > >> > EXEC sp_executesql @SQL, > >> > N'@countynameIN nvarchar(25)', > >> > @countynameIN = @countyname > >> > > >> > >> > >> > >
I mean in the following place: SET @SQL = 'INSERT INTO dbName.dbo.output_' + @outputTable + ' ( countyname ) VALUES ( @countynameIN)' instead of: SET @SQL = 'INSERT INTO dbName.dbo.output_' + @outputTable + ' ( countyname ) VALUES ( @countyname)' -- Sylvain Lafontaine, ing. MVP - Technologies Virtual-PC E-mail: sylvain aei ca (fill the blanks, no spam please) [quoted text, click to view] "Bil Click" <BilClick@discussions.microsoft.com> wrote in message news:CF56C79A-A350-4A2F-B283-F2C39A65F517@microsoft.com... > Thank you Sylvain. If I use this, I get syntax error near '=' > > ALTER PROCEDURE [dbo].[_spInsert_output_all2] > (@outputTable nvarchar(32), > @countyname varchar(25)) > AS > DECLARE @SQL Nvarchar(1000) > SET @SQL = > 'INSERT INTO dbName.dbo.output_' + @outputTable + ' ( countyname ) > VALUES ( @countyname)' > > EXEC sp_executesql @SQL, > N'@countyname nvarchar(25)', > countyname = @countyname > > > > "Sylvain Lafontaine" wrote: > >> You are using @countyname instead of @countynameIN inside your sql >> string. >> >> -- >> Sylvain Lafontaine, ing. >> MVP - Technologies Virtual-PC >> E-mail: sylvain aei ca (fill the blanks, no spam please) >> >> >> "Bil Click" <BilClick@discussions.microsoft.com> wrote in message >> news:685B7372-B042-4FE5-B114-D1671EDC8686@microsoft.com... >> > This sproc is called from Vb.NET code with different outputTable and >> > countyname values. Keep getting error: Must declare the scalar variable >> > "@countyname". >> > >> > ALTER PROCEDURE [dbo].[_spInsert_output_all2] >> > (@outputTable nvarchar(32), >> > @countyname varchar(25)) >> > AS >> > DECLARE @SQL Nvarchar(1000) >> > SET @SQL = >> > 'INSERT INTO dbName.dbo.output_' + @outputTable + ' ( countyname ) >> > VALUES ( @countyname)' >> > >> > EXEC sp_executesql @SQL, >> > N'@countynameIN nvarchar(25)', >> > @countynameIN = @countyname >> > >> >> >>
The syntax is wrong for your SET line, try: SET @SQL = 'INSERT INTO dbName.dbo.output_' + @outputTable + ' (countyname) VALUES (' + @countyname + ')' MH [quoted text, click to view] "Bil Click" <BilClick@discussions.microsoft.com> wrote in message news:CF56C79A-A350-4A2F-B283-F2C39A65F517@microsoft.com... > Thank you Sylvain. If I use this, I get syntax error near '=' > > ALTER PROCEDURE [dbo].[_spInsert_output_all2] > (@outputTable nvarchar(32), > @countyname varchar(25)) > AS > DECLARE @SQL Nvarchar(1000) > SET @SQL = > 'INSERT INTO dbName.dbo.output_' + @outputTable + ' ( countyname ) > VALUES ( @countyname)' > > EXEC sp_executesql @SQL, > N'@countyname nvarchar(25)', > countyname = @countyname > > > > "Sylvain Lafontaine" wrote: > >> You are using @countyname instead of @countynameIN inside your sql >> string. >> >> -- >> Sylvain Lafontaine, ing. >> MVP - Technologies Virtual-PC >> E-mail: sylvain aei ca (fill the blanks, no spam please) >> >> >> "Bil Click" <BilClick@discussions.microsoft.com> wrote in message >> news:685B7372-B042-4FE5-B114-D1671EDC8686@microsoft.com... >> > This sproc is called from Vb.NET code with different outputTable and >> > countyname values. Keep getting error: Must declare the scalar variable >> > "@countyname". >> > >> > ALTER PROCEDURE [dbo].[_spInsert_output_all2] >> > (@outputTable nvarchar(32), >> > @countyname varchar(25)) >> > AS >> > DECLARE @SQL Nvarchar(1000) >> > SET @SQL = >> > 'INSERT INTO dbName.dbo.output_' + @outputTable + ' ( countyname ) >> > VALUES ( @countyname)' >> > >> > EXEC sp_executesql @SQL, >> > N'@countynameIN nvarchar(25)', >> > @countynameIN = @countyname >> > >> >> >>
Bil Click (BilClick@discussions.microsoft.com) writes: [quoted text, click to view] > Thank you Sylvain. If I use this, I get syntax error near '=' > > ALTER PROCEDURE [dbo].[_spInsert_output_all2] > (@outputTable nvarchar(32), > @countyname varchar(25)) > AS > DECLARE @SQL Nvarchar(1000) > SET @SQL = > 'INSERT INTO dbName.dbo.output_' + @outputTable + ' ( countyname ) > VALUES ( @countyname)' > > EXEC sp_executesql @SQL, > N'@countyname nvarchar(25)', > countyname = @countyname
The error is one the very last line, which you would also notice if you care to read the line number in the error message. Besides what is the rationale of having the table name parameterised? This is usually at odds with best practice. And, as you have noticed, it makes programming more difficult, -- 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
Bil declare @sql nvarchar(1000), @col sysname, @orderid int select @col='OrderID', @orderid=10248 set @sql = 'select * from Northwind..Orders where '+@col+'= @ordid' exec sp_executesql @sql, N'@ordid int',@orderid [quoted text, click to view] "Bil Click" <BilClick@discussions.microsoft.com> wrote in message news:5386B636-C3A3-43E8-96D1-002BDB463165@microsoft.com... > Thanks to all of you, but I still cannot get complete syntax correct. My > vb.Net code repeatedly calls this sproc using different tables and > countyname. That is why I was using sp_executesql and dynamic sql. > > I am still confused on how & where to declare & set the variables. If > anyone > could show the complete script I would greatly appreciate it!!! > > "Sylvain Lafontaine" wrote: > >> I mean in the following place: >> >> SET @SQL = >> 'INSERT INTO dbName.dbo.output_' + @outputTable + ' ( countyname ) >> VALUES ( @countynameIN)' >> >> instead of: >> >> SET @SQL = >> 'INSERT INTO dbName.dbo.output_' + @outputTable + ' ( countyname ) >> VALUES ( @countyname)' >> >> -- >> Sylvain Lafontaine, ing. >> MVP - Technologies Virtual-PC >> E-mail: sylvain aei ca (fill the blanks, no spam please) >> >> >> "Bil Click" <BilClick@discussions.microsoft.com> wrote in message >> news:CF56C79A-A350-4A2F-B283-F2C39A65F517@microsoft.com... >> > Thank you Sylvain. If I use this, I get syntax error near '=' >> > >> > ALTER PROCEDURE [dbo].[_spInsert_output_all2] >> > (@outputTable nvarchar(32), >> > @countyname varchar(25)) >> > AS >> > DECLARE @SQL Nvarchar(1000) >> > SET @SQL = >> > 'INSERT INTO dbName.dbo.output_' + @outputTable + ' ( countyname ) >> > VALUES ( @countyname)' >> > >> > EXEC sp_executesql @SQL, >> > N'@countyname nvarchar(25)', >> > countyname = @countyname >> > >> > >> > >> > "Sylvain Lafontaine" wrote: >> > >> >> You are using @countyname instead of @countynameIN inside your sql >> >> string. >> >> >> >> -- >> >> Sylvain Lafontaine, ing. >> >> MVP - Technologies Virtual-PC >> >> E-mail: sylvain aei ca (fill the blanks, no spam please) >> >> >> >> >> >> "Bil Click" <BilClick@discussions.microsoft.com> wrote in message >> >> news:685B7372-B042-4FE5-B114-D1671EDC8686@microsoft.com... >> >> > This sproc is called from Vb.NET code with different outputTable and >> >> > countyname values. Keep getting error: Must declare the scalar >> >> > variable >> >> > "@countyname". >> >> > >> >> > ALTER PROCEDURE [dbo].[_spInsert_output_all2] >> >> > (@outputTable nvarchar(32), >> >> > @countyname varchar(25)) >> >> > AS >> >> > DECLARE @SQL Nvarchar(1000) >> >> > SET @SQL = >> >> > 'INSERT INTO dbName.dbo.output_' + @outputTable + ' ( countyname ) >> >> > VALUES ( @countyname)' >> >> > >> >> > EXEC sp_executesql @SQL, >> >> > N'@countynameIN nvarchar(25)', >> >> > @countynameIN = @countyname >> >> > >> >> >> >> >> >> >> >> >>
Bil Click (BilClick@discussions.microsoft.com) writes: [quoted text, click to view] > Thanks to all of you, but I still cannot get complete syntax correct. My > vb.Net code repeatedly calls this sproc using different tables and > countyname. That is why I was using sp_executesql and dynamic sql.
But why are there multiple tables? Why not one table which is keyed by whatever you pass in @outputTable? Therein lies your problem. If there are just a few such tables, why not one stored procedure for each table? If the design is really so horrible that there are many of them - naybe it's best not to bother with stored procedures at all? [quoted text, click to view] > I am still confused on how & where to declare & set the variables. If > anyone could show the complete script I would greatly appreciate it!!!
Think of what you pass to sp_executesql as anonymous stored procedure. The surrounding parameters has its set of parameters, and the inner procedure has its set, defined by the second parameter to sp_executesql. They could have the same name (which probably is the most common), or they could have different name. -- 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
ALTER PROCEDURE [dbo].[_spInsert_output_all2] @outputTable nvarchar(32), @countyname nvarchar(25) AS DECLARE @sql nvarchar(1000),@cty nvarchar(25) SET @cty = @countyname SET @SQL = 'INSERT INTO dbName.dbo.output_' + @outputTable + ' ( countyname ) ' + ' VALUES (' + @cty + ') ' exec sp_executesql @sql, @params = N'@outputTable nvarchar(32), @countyname nvarchar(25)', @param1=@outputTable, @param2=@cty I must have a mental block on this problem! Thought I had the code this time but now error is "The name "Camden" is not permitted in this context". 'Camden' was input parameter value for @countyname & ''click' was @outputTable. Erland, there are 70 - 80 output tables and the number varies. Legacy design that does have *some* advantages. I am trying to consolidate stored procedures for each table into one. Thanks again - Bil [quoted text, click to view] "Erland Sommarskog" wrote: > Bil Click (BilClick@discussions.microsoft.com) writes: > > Thanks to all of you, but I still cannot get complete syntax correct. My > > vb.Net code repeatedly calls this sproc using different tables and > > countyname. That is why I was using sp_executesql and dynamic sql. > > But why are there multiple tables? Why not one table which is keyed > by whatever you pass in @outputTable? Therein lies your problem. > > If there are just a few such tables, why not one stored procedure for each > table? If the design is really so horrible that there are many of them - > naybe it's best not to bother with stored procedures at all? > > > I am still confused on how & where to declare & set the variables. If > > anyone could show the complete script I would greatly appreciate it!!! > > Think of what you pass to sp_executesql as anonymous stored procedure. > The surrounding parameters has its set of parameters, and the inner > procedure has its set, defined by the second parameter to sp_executesql. > They could have the same name (which probably is the most common), or > they could have different name. > > > -- > 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
OK, I finally got the right combination! Many Thanks again to all! ALTER PROCEDURE [dbo].[_spInsert_output_all2] @outputTable nvarchar(32), @countyname nvarchar(25) AS DECLARE @sql nvarchar(1000) SET @SQL = 'INSERT INTO dbo.output_' + @outputTable + ' ( countyname) ' +' VALUES (@cty) ' exec sp_executesql @sql,N'@cty nvarchar(25)',@countyname ----------------------------------------------------------------------------- [quoted text, click to view] "Bil Click" wrote: > ALTER PROCEDURE [dbo].[_spInsert_output_all2] > @outputTable nvarchar(32), > @countyname nvarchar(25) > AS > DECLARE @sql nvarchar(1000),@cty nvarchar(25) > SET @cty = @countyname > SET @SQL = 'INSERT INTO dbName.dbo.output_' + @outputTable + ' ( countyname > ) ' + > ' VALUES (' + @cty + ') ' > > exec sp_executesql @sql, > @params = N'@outputTable nvarchar(32), @countyname nvarchar(25)', > @param1=@outputTable, @param2=@cty > > I must have a mental block on this problem! Thought I had the code this time > but now error is "The name "Camden" is not permitted in this context". > 'Camden' was input parameter value for @countyname & ''click' was > @outputTable. > > Erland, there are 70 - 80 output tables and the number varies. Legacy design > that does have *some* advantages. I am trying to consolidate stored > procedures for each table into one. > > Thanks again - Bil > > "Erland Sommarskog" wrote: > > > Bil Click (BilClick@discussions.microsoft.com) writes: > > > Thanks to all of you, but I still cannot get complete syntax correct. My > > > vb.Net code repeatedly calls this sproc using different tables and > > > countyname. That is why I was using sp_executesql and dynamic sql. > > > > But why are there multiple tables? Why not one table which is keyed > > by whatever you pass in @outputTable? Therein lies your problem. > > > > If there are just a few such tables, why not one stored procedure for each > > table? If the design is really so horrible that there are many of them - > > naybe it's best not to bother with stored procedures at all? > > > > > I am still confused on how & where to declare & set the variables. If > > > anyone could show the complete script I would greatly appreciate it!!! > > > > Think of what you pass to sp_executesql as anonymous stored procedure. > > The surrounding parameters has its set of parameters, and the inner > > procedure has its set, defined by the second parameter to sp_executesql. > > They could have the same name (which probably is the most common), or > > they could have different name. > > > > > > -- > > 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
Don't see what you're looking for? Try a search.
|
|
|