Get program table name for Store procedure

You can get the table name from the program.

Like the following code:

Create Procedure [dbo].[sp_SelectAll] @BankName nvarchar(50) As Begin Select * From @BankName End 

This is an error code.

Is there another way?

+4
source share
3 answers

you can do it like:

exec ('select * from' + @BankName)

but it is not a good practice to use this.

You can learn more about dynamic sql from:

http://www.mssqltips.com/tip.asp?tip=1160

http://www.sqlteam.com/article/introduction-to-dynamic-sql-part-1

+5
source

Tables cannot be parameterized. You have to write dynamic sql ie generate sql strings and execute it with exec.

exec ('select * from' + @BankName)

0
source

try it

  Create Procedure [dbo].[sp_SelectAll] @BankName nvarchar(50) As Begin EXEC('Select * from '+ @BankName) End 

You need to work with dynamic SQL to do this correctly.

0
source

Source: https://habr.com/ru/post/1339784/


All Articles