加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > MsSql教程 > 正文

mssql存储过程表名和字段名为变量的实现方法

发布时间:2020-12-12 09:28:09 所属栏目:MsSql教程 来源:网络整理
导读:没有使用动态语句直接报错 错误的 div class="codetitle" a style="CURSOR: pointer" data="88372" class="copybut" id="copybut88372" onclick="doCopy('code88372')" 代码如下:div class="codebody" id="code88372" alter proc testpapers as begin declar

没有使用动态语句直接报错 错误的
<div class="codetitle"><a style="CURSOR: pointer" data="88372" class="copybut" id="copybut88372" onclick="doCopy('code88372')"> 代码如下:<div class="codebody" id="code88372">
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

消息 156,级别 15,状态 1,过程 testpapers,第 1 行
关键字 'select' 附近有语法错误。
消息 1087,级别 15,状态 2,过程 testpapers,第 1 行
必须声明表变量 "@tems"。 首先要让表名或者字段为变量则要用到动态语句 错误的
<div class="codetitle"><a style="CURSOR: pointer" data="80086" class="copybut" id="copybut80086" onclick="doCopy('code80086')"> 代码如下:<div class="codebody" id="code80086">
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

消息 1087,级别 15,状态 2,第 1 行
必须声明表变量 "@tems"。 将表名和字段名写到exec里边 正确的
<div class="codetitle"><a style="CURSOR: pointer" data="58060" class="copybut" id="copybut58060" onclick="doCopy('code58060')"> 代码如下:<div class="codebody" id="code58060">
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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读