在存储过程如何执行@strSql中的语句?在线等!正确立刻结帖!

niukl 2003-09-11 04:10:32
我想实现一个分各种条件查询的查询语句:比如:select * from table1 where 条件1 and 条件2 and 条件3 and 条件4 ......
我原想有一个变量:
declare @strSql char(300)
set @strSql='select * from table1'
if ....
begin
set @strSql=@strS1l+ '条件1'
end
if ....
begin
set @strSql=@strS1l+ '条件2'
end
.
.
.

最后用一个语句执行@strSql,想法不知可行吗?不行的话,如何才能实验呀?
...全文
38 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
niukl 2003-09-11
  • 打赏
  • 举报
回复
谢谢大家!我为什么结不了帖呀!现在分都怎么分呀?
sjw800614 2003-09-11
  • 打赏
  • 举报
回复
--直接执行 SQL语句即可
--只是在 转化SQL语句时,需,注意 几点,
--主要是 SQL中 单引号的处理
CREATE PROCEDURE usp_GetForTest
@t1 NVARCHAR(10)='' --名称
AS
SET NOCOUNT ON
DECLARE @charSQL VARCHAR(4000)
SET @charSQL='SELECT * FROM sysobjects WHERE xtype=''V''' --查询视图
SET @charSQL=@charSQL + ' AND name=''' + @t1 + ''''
--可以填加需要的其他条件

EXECUTE (@charSQL) --必须使用括号

--错误处理
--返回即可
GO

aierong 2003-09-11
  • 打赏
  • 举报
回复
用这个就不会这么麻烦啦 exec sp_executesql @strsql

它可以参数替换
niukl 2003-09-11
  • 打赏
  • 举报
回复
马上结帖,大家的解答都有分!
niukl 2003-09-11
  • 打赏
  • 举报
回复
declare @user_tel char(11)
declare @strSql char(200)
set @user_tel='111111111'
if @user_tel is not null
begin
set @strSql='where user_tel='''+ rtrim(@user_tel) + '''' -- and jinjicd='11111111'
end
set @strSql='select * from t_usertousu ' + @strSql
select @strSql
exec (@strSql)

通过,原来真是单引号的错!能不能给解释一下"" 和''和C语言中的有什么不同吗?
niukl 2003-09-11
  • 打赏
  • 举报
回复
谢谢大家,中秋快乐呀?有谁留一下QQ号吧,我:6180220
dafu71 2003-09-11
  • 打赏
  • 举报
回复
declare @user_tel char(11)
declare @strSql char(200)
set @user_tel='111111111'
if @user_tel is not null
begin
set @strSql='where user_tel='+ @user_tel -- and jinjicd='11111111'
end
set @strSql='select * from t_usertousu ' + @strSql
select @strSql
exec (@strSql)

这个为什么不能行呀
---------try

declare @user_tel char(11)
declare @strSql char(200)
set @user_tel='111111111'
if @user_tel is not null
begin
set @strSql='where user_tel='''+ rtrim(@user_tel) + '''' -- and jinjicd='11111111'
end
set @strSql='select * from t_usertousu ' + @strSql
select @strSql
exec (@strSql)
niukl 2003-09-11
  • 打赏
  • 举报
回复
select @strSql

结果是:
select * from t_usertousu where user_tel=111111111
我的user_tel 是字符串,这个数字也不行呀!
"" 和 '' 括起来的有什么区别呀?
aierong 2003-09-11
  • 打赏
  • 举报
回复
declare @strSql char(300)
set @strSql='select * from table1'
if ....
begin
set @strSql=@strS1l+ '条件1'
end
if ....
begin
set @strSql=@strS1l+ '条件2'
end
exec sp_executesql @strsql
sdhdy 2003-09-11
  • 打赏
  • 举报
回复
可以呀!
declare @strSql char(8000)
set @strSql='select * from table1'
if ....
begin
set @strSql=@strS1l+ '条件1'
end
if ....
begin
set @strSql=@strS1l+ '条件2'
end
.
exec (@strSql) --add
friendliu 2003-09-11
  • 打赏
  • 举报
回复
exec(@strsql)
CrazyFor 2003-09-11
  • 打赏
  • 举报
回复
exec(@strsql)
niukl 2003-09-11
  • 打赏
  • 举报
回复
declare @user_tel char(11)
declare @strSql char(200)
set @user_tel='111111111'
if @user_tel is not null
begin
set @strSql='where user_tel='+ @user_tel -- and jinjicd='11111111'
end
set @strSql='select * from t_usertousu ' + @strSql
select @strSql
exec (@strSql)

这个为什么不能行呀
dlkfth 2003-09-11
  • 打赏
  • 举报
回复

exec('select getdate()') 或
sp_executesql
执行可以多次重用或动态生成的 Transact-SQL 语句或批处理。Transact-SQL 语句或批处理可以包含嵌入参数。

语法
sp_executesql [@stmt =] stmt
[
{, [@params =] N'@parameter_name data_type [,...n]' }
{, [@param1 =] 'value1' [,...n] }
]

参数
[@stmt =] stmt

包含 Transact-SQL 语句或批处理的 Unicode 字符串,stmt 必须是可以隐式转换为 ntext 的 Unicode 常量或变量。不允许使用更复杂的 Unicode 表达式(例如使用 + 运算符串联两个字符串)。不允许使用字符常量。如果指定常量,则必须使用 N 作为前缀。例如,Unicode 常量 N'sp_who' 是有效的,但是字符常量 'sp_who' 则无效。字符串的大小仅受可用数据库服务器内存限制。

stmt 可以包含与变量名形式相同的参数,例如:

N'SELECT * FROM Employees WHERE EmployeeID = @IDParameter'

stmt 中包含的每个参数在 @params 参数定义列表和参数值列表中均必须有对应项。

[@params =] N'@parameter_name data_type [,...n]'

字符串,其中包含已嵌入到 stmt 中的所有参数的定义。该字符串必须是可以隐式转换为 ntext 的 Unicode 常量或变量。每个参数定义均由参数名和数据类型组成。n 是表明附加参数定义的占位符。stmt 中指定的每个参数都必须在 @params 中定义。如果 stmt 中的 Transact-SQL 语句或批处理不包含参数,则不需要 @params。该参数的默认值为 NULL。

[@param1 =] 'value1'

参数字符串中定义的第一个参数的值。该值可以是常量或变量。必须为 stmt 中包含的每个参数提供参数值。如果 stmt 中包含的 Transact-SQL 语句或批处理没有参数,则不需要值。

n

附加参数的值的占位符。这些值只能是常量或变量,而不能是更复杂的表达式,例如函数或使用运算符生成的表达式。

返回代码值
0(成功)或 1(失败)

结果集
从生成 SQL 字符串的所有 SQL 语句返回结果集。

注释
在批处理、名称作用域和数据库上下文方面,sp_executesql 与 EXECUTE 的行为相同。sp_executesql stmt 参数中的 Transact-SQL 语句或批处理在执行 sp_executesql 语句时才编译。然后编译 stmt 中的内容并作为执行计划运行(独立于名为 sp_executesql 的批处理的执行计划)。sp_executesql 批处理不能引用调用 sp_executesql 的批处理中声明的变量。sp_executesql 批处理中的本地游标和变量对调用 sp_executesql 的批处理是不可见的。对数据库上下文所作的更改只在 sp_executesql 语句结束前有效。

如果只更改了语句中的参数值,则 sp_executesql 可用来代替存储过程多次执行 Transact-SQL 语句。因为 Transact-SQL 语句本身保持不变仅参数值变化,所以 Microsoft® SQL Server™ 查询优化器可能重复使用首次执行时所生成的执行计划。



说明 如果语句字符串中的对象名不是全限定名,则该执行计划不会被重用。


sp_executesql 支持与 Transact-SQL 字符串相独立的参数值的设置:

DECLARE @IntVariable INT
DECLARE @SQLString NVARCHAR(500)
DECLARE @ParmDefinition NVARCHAR(500)

/* Build the SQL string once.*/
SET @SQLString =
N'SELECT * FROM pubs.dbo.employee WHERE job_lvl = @level'
SET @ParmDefinition = N'@level tinyint'
/* Execute the string with the first parameter value. */
SET @IntVariable = 35
EXECUTE sp_executesql @SQLString, @ParmDefinition,
@level = @IntVariable
/* Execute the same string with the second parameter value. */
SET @IntVariable = 32
EXECUTE sp_executesql @SQLString, @ParmDefinition,
@level = @IntVariable

替换 sp_executesql 中的参数的能力,与使用 EXECUTE 语句执行字符串相比,有下列优点:

因为在 sp_executesql 中,Transact-SQL 语句的实际文本在两次执行之间未改变,所以查询优化器应该能将第二次执行中的 Transact-SQL 语句与第一次执行时生成的执行计划匹配。这样,SQL Server 不必编译第二条语句。


Transact-SQL 字符串只生成一次。


整型参数按其本身格式指定。不需要转换为 Unicode。
权限
执行权限默认授予 public 角色。

示例
ghost555 2003-09-11
  • 打赏
  • 举报
回复
变量名误写了..where那去了?
exec()
aierong 2003-09-11
  • 打赏
  • 举报
回复
exec sp_executesql @strsql
arrow_gx 2003-09-11
  • 打赏
  • 举报
回复
@strsql='select * from youtTable'
exec(@strsql)
pengdali 2003-09-11
  • 打赏
  • 举报
回复
如:


exec('select * from xx where 1=1')
pengdali 2003-09-11
  • 打赏
  • 举报
回复
exec(@strsql)

22,206

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧