您现在的位置是:网站首页> 编程资料编程资料
mssql存储过程表名和字段名为变量的实现方法_MsSql_
2023-05-26
399人已围观
简介 mssql存储过程表名和字段名为变量的实现方法_MsSql_
没有使用动态语句直接报错
错误的
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"。
首先要让表名或者字段为变量则要用到动态语句
错误的
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里边
正确的
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
错误的
复制代码 代码如下:
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"。
首先要让表名或者字段为变量则要用到动态语句
错误的
复制代码 代码如下:
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里边
正确的
复制代码 代码如下:
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
您可能感兴趣的文章:
相关内容
- 通过T-SQL语句实现数据库备份与还原的代码_MsSql_
- 卸载VS2011 Developer Preview后Sql Server2008 R2建立数据库关系图报“找不到指定的模块”错误的解决方法_MsSql_
- sqlserver 临时表 Vs 表变量 详细介绍_MsSql_
- 扩展性很好的一个分页存储过程分享_MsSql_
- 自动定时备份sqlserver数据库的方法_MsSql_
- sql添加数据后返回受影响行数据_MsSql_
- 自己收集比较强大的分页存储过程 推荐_MsSql_
- MSSQL优化之探索MSSQL执行计划(转)_MsSql_
- 运行asp.net程序 报错:磁盘空间不足_MsSql_
- 在数据库中自动生成编号的实现方法分享_MsSql_
