Author |
Topic |
desikankannan
Posting Yak Master
152 Posts |
Posted - 2010-11-19 : 05:10:16
|
Hi,iam trying to outparm in stored procedure but its not working,iam daynamically passing fields and table name and i want to getthe keyid from thaterror:Msg 156, Level 15, State 1, Procedure getkeyid, Line 16Incorrect syntax near the keyword 'exec'.USE [smartsystem]GO/****** Object: StoredProcedure [dbo].[getkeyid] Script Date: 11/19/2010 15:29:35 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOALTER procedure [dbo].[getkeyid](@fldgaugeid varchar(70),@tablename varchar(50),@keyfield varchar(50),@descvalue varchar(75),@getid int output)as set nocount ondeclare @sql nvarchar(max)select @sql = 'select ' + @keyfield + ' from ' + @tablename + ' where ' + '''' +@descvalue+ '''' + ' = ' + @fldgaugeidSELECT @getid= exec(@sql)Desikankannan |
|
pk_bohra
Master Smack Fu Yak Hacker
1182 Posts |
Posted - 2010-11-19 : 05:12:10
|
have a look at sp_executesql |
 |
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2010-11-19 : 05:15:43
|
Also read thiswww.sommarskog.se/dynamic_sql.htmlMadhivananFailing to plan is Planning to fail |
 |
|
nigelrivett
Master Smack Fu Yak Hacker
3385 Posts |
Posted - 2010-11-19 : 05:23:56
|
probablyselect @sql ='select @getid = ' + @keyfield + ' from ' + @tablename + ' where ' + '''' +@descvalue+ '''' + ' = ' + @fldgaugeidexec sp_executesql @sql, N'@getid int out', @getid outseehttp://www.nigelrivett.net/SQLTsql/sp_executeSQL.html==========================================Cursors are useful if you don't know sql.SSIS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
desikankannan
Posting Yak Master
152 Posts |
Posted - 2010-11-19 : 05:31:42
|
quote: Originally posted by pk_bohra have a look at sp_executesql
below line is not working,SELECT @getid= exec(@sql)Desikankannan |
 |
|
nigelrivett
Master Smack Fu Yak Hacker
3385 Posts |
Posted - 2010-11-19 : 05:40:45
|
That's because you can't pipe the result of dynamic sql into a variable - you will only get the return code of the execution.You could create a table variable aninsert @tblexec(@sql)select @get_id = get_id from @tblhave a look at the link I gave for sp_executesql or look in bol.sp_executesql allows you to pass and receive parameter values into dynamic sql.The code I gave should be pretty close.==========================================Cursors are useful if you don't know sql.SSIS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
desikankannan
Posting Yak Master
152 Posts |
Posted - 2010-11-19 : 05:52:17
|
quote: Originally posted by desikankannan
quote: Originally posted by pk_bohra have a look at sp_executesql
below line is not working,SELECT @getid= exec(@sql)Desikankannan
iam not getting this line can u explainexec sp_executesql @sql, N'@getid int out', @getid outDesikankannan |
 |
|
desikankannan
Posting Yak Master
152 Posts |
Posted - 2010-11-19 : 06:03:43
|
quote: Originally posted by nigelrivett That's because you can't pipe the result of dynamic sql into a variable - you will only get the return code of the execution.You could create a table variable aninsert @tblexec(@sql)select @get_id = get_id from @tblhave a look at the link I gave for sp_executesql or look in bol.sp_executesql allows you to pass and receive parameter values into dynamic sql.The code I gave should be pretty close.==========================================Cursors are useful if you don't know sql.SSIS can be used in a similar way.Beer is not cold and it isn't fizzy.
Hi,i tried ur code but it throw errorMsg 1087, Level 15, State 2, Procedure getkeyid, Line 18Must declare the table variable "@tbl".USE [smartsystem]GO/****** Object: StoredProcedure [dbo].[getkeyid] Script Date: 11/19/2010 15:29:35 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOALTER procedure [dbo].[getkeyid](@fldgaugeid varchar(70),@tablename varchar(50),@keyfield varchar(50),@descvalue varchar(75),@getid int output)as set nocount ondeclare @sql nvarchar(max), @tbl nvarchar(200)select @sql ='select @getid = ' + @keyfield + ' from ' + @tablename + ' where ' + '''' +@descvalue+ '''' + ' = ' + @fldgaugeidinsert @tbl exec(@sql)Desikankannan |
 |
|
nigelrivett
Master Smack Fu Yak Hacker
3385 Posts |
Posted - 2010-11-19 : 06:09:11
|
declare @sql nvarchar(max)declare @tbl table (get_id int)select @sql ='select getid = ' + @keyfield + ' from ' + @tablename + ' where ' + '''' +@descvalue+ '''' + ' = ' + @fldgaugeidinsert @tbl exec(@sql)selet @get_id = get_id from @tblsp_executesql is easier to return a single value.==========================================Cursors are useful if you don't know sql.SSIS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
pk_bohra
Master Smack Fu Yak Hacker
1182 Posts |
Posted - 2010-11-19 : 06:34:33
|
quote: Originally posted by nigelrivettinsert @tbl exec(@sql)selet @get_id = get_id from @tbl
I feel that this is not supported. |
 |
|
desikankannan
Posting Yak Master
152 Posts |
Posted - 2010-11-19 : 06:36:34
|
quote: Originally posted by nigelrivett declare @sql nvarchar(max)declare @tbl table (get_id int)select @sql ='select getid = ' + @keyfield + ' from ' + @tablename + ' where ' + '''' +@descvalue+ '''' + ' = ' + @fldgaugeidinsert @tbl exec(@sql)selet @get_id = get_id from @tblsp_executesql is easier to return a single value.==========================================Cursors are useful if you don't know sql.SSIS can be used in a similar way.Beer is not cold and it isn't fizzy.
hi,i tried ur code it throws error to meerrorConversion failed when converting the varchar value 'fldrefmstcode' to data type int.USE [smartsystem]GO/****** Object: StoredProcedure [dbo].[getkeyid] Script Date: 11/19/2010 15:29:35 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOALTER procedure [dbo].[getkeyid](@fldgaugeid varchar(70),@tablename varchar(50),@keyfield varchar(50),@descvalue varchar(75))as set nocount ondeclare @sql nvarchar(max),@gets_id intdeclare @tbl table (get_id int)select @sql ='select getid = ' + @keyfield + ' from ' + @tablename + ' where ' + '''' +@descvalue+ '''' + ' = ' + @fldgaugeidinsert @tbl exec(@sql)select @gets_id = get_id from @tblprint @gets_idexec getkeyid '3048','Std_mstmasterref','fldmstrefid','fldrefmstcode'Desikankannan |
 |
|
pk_bohra
Master Smack Fu Yak Hacker
1182 Posts |
Posted - 2010-11-19 : 06:39:53
|
An example for your understandingdeclare @sql nvarchar(4000)declare @tbl table (get_id int)declare @tablename nvarchar(4000)declare @descvalue nvarchar(4000)declare @fldgaugeid nvarchar(4000)declare @getid intset @tablename ='sysobjects'set @descvalue = 'id 'set @fldgaugeid = '6'select @sql ='select getid = count(*) from ' + @tablename + ' where ' + '' +@descvalue+ '' + ' = ' + @fldgaugeid exec sp_executesql @sql, N'@getid int output', @getid outputPrint @getidI suggest that you understand the example as it will be helpful for you in future.--Edit:Try:select @sql ='select getid = ' + @keyfield + ' from ' + @tablename + ' where ' + '' +@descvalue+ '' + ' = ' + @fldgaugeidexec sp_executesql @sql, N'@getid int output', @getid output |
 |
|
nigelrivett
Master Smack Fu Yak Hacker
3385 Posts |
Posted - 2010-11-19 : 06:42:44
|
Can you post @sql that is executed please.I'm guessing that either @keyfield is not an integer column or that @fldgaugeid is an integer and @descvalue contains 'fldrefmstcode'.==========================================Cursors are useful if you don't know sql.SSIS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
pk_bohra
Master Smack Fu Yak Hacker
1182 Posts |
Posted - 2010-11-19 : 06:46:22
|
quote: Originally posted by nigelrivett Can you post @sql that is executed please.I'm guessing that either @keyfield is not an integer column or that @fldgaugeid is an integer and @descvalue contains 'fldrefmstcode'.==========================================Cursors are useful if you don't know sql.SSIS can be used in a similar way.Beer is not cold and it isn't fizzy.
One of the reason that OP is getting error because of '''' +@descvalue+ '''' . This results in string and column name in where condition should not be in single quotes. |
 |
|
desikankannan
Posting Yak Master
152 Posts |
Posted - 2010-11-19 : 07:06:44
|
quote: Originally posted by nigelrivett Can you post @sql that is executed please.I'm guessing that either @keyfield is not an integer column or that @fldgaugeid is an integer and @descvalue contains 'fldrefmstcode'.==========================================Cursors are useful if you don't know sql.SSIS can be used in a similar way.Beer is not cold and it isn't fizzy.
iam passing parameter like thisexec getkeyid '3048','Std_mstmasterref','fldmstrefid','fldrefmstcode'i getting errorConversion failed when converting the varchar value 'fldrefmstcode' to data type int.select @sql ='select getid = ' + @keyfield + ' from ' + @tablename + ' where ' + '''' +@descvalue+ '''' + ' = ' + @fldgaugeidDesikankannan |
 |
|
desikankannan
Posting Yak Master
152 Posts |
Posted - 2010-11-19 : 07:16:24
|
quote: Originally posted by nigelrivett Can you post @sql that is executed please.I'm guessing that either @keyfield is not an integer column or that @fldgaugeid is an integer and @descvalue contains 'fldrefmstcode'.==========================================Cursors are useful if you don't know sql.SSIS can be used in a similar way.Beer is not cold and it isn't fizzy.
thanks working wellDesikankannan |
 |
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2010-11-19 : 07:48:54
|
quote: Originally posted by pk_bohra
quote: Originally posted by nigelrivettinsert @tbl exec(@sql)selet @get_id = get_id from @tbl
I feel that this is not supported.
It is supportedMadhivananFailing to plan is Planning to fail |
 |
|
|