Click here to Skip to main content
16,020,162 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
please give solution of the test SP below :

create procedure TestSP(@ExistTable nvarchar(128),@NewTable nvarchar(128))
as
begin
select * into @NewTable from @ExistTable
end

Error shows
"Msg 102, Level 15, State 1, Procedure TestSP, Line 5
Incorrect syntax near '@NewTable'."


if I use
create procedure TestSP(@ExistTable nvarchar(128),@NewTable nvarchar(128))
as
begin
select * into [@NewTable] from [@ExistTable]
end

Then SP creates but not execute and the error shows:

exec TestSP ('test2','test1')

"Msg 102, Level 15, State 1, Line 1
Incorrect syntax near 'test2'."


Please help me out.....
Posted
Comments
[no name] 18-Jul-15 15:32pm    
http://www.w3schools.com/sql/default.asp
RTK The Limited Edition 18-Jul-15 16:19pm    
I don't see the answer here. Please give the SOLUTION (THE SP)at your earliest
[no name] 19-Jul-15 8:02am    
absolutely! you start from the first page. You are at zero level in SQL right now. Learn it! Create it! don't be silly!

1 solution

In most SQL queries you can not use variables (the most common reason is age)...
What you have to do is create a dynamic SQL...
Something like this:
SQL
DECLARE @SQL AS NVARCHAR(MAX)
SET @SQL = "select * into " + @NewTable + " from " + @ExistTable
EXEC(@SQL)


https://msdn.microsoft.com/en-us/library/ms188001.aspx[^]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900