1

hello frnds i need to pass a table name as parameter to stored procedure

CREATE PROCEDURE six @tablename nvarchar AS SELECT * FROM + @tablename Go exec six Entry_sixsigma_mag 

it gives error like

Msg 102, Level 15, State 1, Procedure six, Line 3 Incorrect syntax near '+'. Msg 208, Level 16, State 1, Procedure six, Line 3 Invalid object name '@sixsigma'.

1

1 Answer 1

4

Try something like

CREATE PROCEDURE six @tablename nvarchar(100) AS EXEC('SELECT * FROM ' + @tablename) Go exec six Entry_sixsigma_mag 

Have a look at EXECUTE (Transact-SQL)

But you should also have a look at

before using this blindly.

Sign up to request clarification or add additional context in comments.

3 Comments

hey it gives error Msg 208, Level 16, State 1, Line 1 Invalid object name 'E'.
You need to change nvarchar to nvarchar(100) or what ever your length is you need. You have to remember that if you do not specify the length of a NVARCHAR/VARCHAR it will assume 1.
Instead of EXEC('...') you can also use sp_executesql: msdn.microsoft.com/en-us/library/ms188001.aspx

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.