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.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Query from stored procedure with parameter

Author  Topic 

marcobonfante
Starting Member

1 Post

Posted - 2014-07-04 : 05:18:24
Hi all guys i have this problem:

i need to execute a stored procedure with a parameter on a part of the table name like this:

SELECT @TSQL='SELECT dbo.ponte_codice_cassa.nome_cass
, dbo.food03.id_counter
, dbo.food03.data
, dbo.food03.barcode
, dbo.food03.descrizione
, dbo.food03.codice_articolo
, dbo.food03.iva

the table name food03 is the parameter i need to change but i would like this

because i need to recall differente table from last 2 character like
food01-->01 is the parameter
food02-->02 is the parameter
food03-->03 is the parameter

dbo.food@parameter is that possible??

thanks

Marco

gbritton
Master Smack Fu Yak Hacker

2780 Posts

Posted - 2014-07-04 : 08:37:08
Sure. Pass the suffix as a char(2); build the query as an NVARCHAR(MAX) variable; execute the result. e.g.


create proc myproc @suffix char(2)
as
declare @sql nvarchar(max) = '
select * from mytable' + @suffix
exec(@sql)


then run it like this:


exec myproc '01'
Go to Top of Page
   

- Advertisement -