No direct error using dynamic statements
of the wrong
Copy Code code as follows:
ALTER PROC Testpapers
As
Begin
declare @tems nvarchar (max), @zidaun nvarchar (max)
Set @tems =select * from @tems ORDER by @zidaun
EXEC (@tems)
End
EXEC testpapers
Message 156, Level 15, State 1, Process testpapers, line 1th
There are syntax errors near the keyword ' select '.
Message 1087, Level 15, State 2, Process testpapers, line 1th
The table variable "@tems" must be declared.
To get a table name or field to be a variable, use the dynamic statement first
of the wrong
Copy Code code as follows:
ALTER PROC Testpapers
As
Begin
declare @tems nvarchar (max), @zidaun nvarchar (max)
Set @tems = ' SELECT * from @tems order by @zidaun ';
EXEC (@tems)
End
EXEC testpapers
Message 1087, Level 15, State 2, line 1th
The table variable "@tems" must be declared.
Write the table name and field name to exec inside
Right to
Copy Code code as follows:
ALTER PROC Testpapers
As
Begin
declare @startRow nvarchar (max), @tems nvarchar (max), @zidaun nvarchar (max)
Set @startRow = ' temp '
Set @tems = ' select * from ';
Set @zidaun = ' p_id ';
EXEC (@tems + @startRow + ' ORDER BY ' + @zidaun)
End
EXEC testpapers