DECLARE @Name nvarchar(255),@Sql nvarchar(4000),@value varchar(50) set @value='查询' DECLARE TName CURSOR FOR SELECT [name] FROM sysobjects WHERE xtype = 'U' OPEN TName FETCH NEXT FROM TName INTO @Name WHILE @@FETCH_STATUS = 0 BEGIN SET @Sql='SELECT * FROM '+@name + 'WHERE STCD='''+@value+'''' EXEC(@Sql) FETCH NEXT FROM TName INTO @name END CLOSE TName DEALLOCATE TName
注意语法
追问
消息 102,级别 15,状态 1,第 1 行 '=' 附近有语法错误。 还是提示有错误啊?
追答
SET @Sql='SELECT * FROM '+@name + ' WHERE STCD='''+@value+'''' 这8一行注0意where5关键字的09前6面空33格7