【发布时间】:2020-01-05 09:34:26
【问题描述】:
我已经创建了一个存储过程,我想在其中将一个 rowid 和表名从选择查询设置为一个变量,但是当我在 sql 中执行我的代码时,会出现一堆错误
我已经 decalre 一个变量,但它一直对 decalare table 说
这是我写的代码
USE [DB_A4BA76_logistic]
GO
CREATE PROCEDURE RecoverLogs @id int
AS
declare @TableName nvarchar(50)
declare @RowId int
set @TableName= ( select Logs.tableName from logs where Logs.id=@id)
set @Rowid= ( select Logs.Rowid from logs where id=@id)
Update @TableName set UpdateStatus='0' where id = @Rowid
if(select UpdateStatus from @TableName where id = @Rowid) = '0'
select 1
else
select 2
GO;
执行上述代码后出现这些错误
Msg 1087, Level 16, State 1, Procedure RecoverLogs, Line 7
Must declare the table variable "@TableName".
Msg 1087, Level 16, State 1, Procedure RecoverLogs, Line 8
Must declare the table variable "@TableName".
【问题讨论】:
-
不能用变量/表达式来替换对象的值;它必须是文字。您必须在此处使用动态 SQL。
-
你为什么不使用 select into ?
-
你如何使用动态对象 @AmiraBedhiafi
SELECT....INTO? -
我使用了 Select .... into 但仍然出现错误 SELECT Logs.tableName INTO @TableName FROM logs WHERE Logs.id=50 以下错误出现 Msg 102, Level 15, State 1, Line 1 不正确'@TableName' 附近的语法。 Msg 137, Level 15, State 2, Line 2 必须声明标量变量“@TableName”。
-
@MoiezHussain 我以为你将数据存储在表中。
标签: sql-server database tsql stored-procedures