设为首页收藏本站Access中国

Office中国论坛/Access中国论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

12下一页
返回列表 发新帖
查看: 5126|回复: 17
打印 上一主题 下一主题

用變量構造出來的SQL語句在TSQL中怎樣作才能執行哪?

[复制链接]
跳转到指定楼层
1#
发表于 2002-10-27 00:52:00 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
declare result as varchar(600)
result = "select * from uv_wage_month_total_union_history where wage_date_year="+str(@my_year)+" and wage_date_month="+ str(@my_date_month)
----------------------------------------
請問現在構造完了,但怎樣作此語句才可以執行哪?有點迷啦。請各位指教,多謝![em15]
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 分享淘帖 订阅订阅
2#
 楼主| 发表于 2002-10-28 17:42:00 | 只看该作者
create procedure usp_qry_xwage_xmonth_en @my_year smallint,@my_month tinyint,@my_dept_str  varchar(200) = null,@my_title_str varchar(200) = null,@my_emp_sn_str varchar(200)=null
as
begin
declare @result  varchar(600)
  set @result = "select * from uv_wage_month_total_union_history where wage_date_year="+str(@my_year)+" and wage_date_month="+ str(@my_month)
if @my_dept_str is not  null
          @result = @result +  "and dept_no in ("+@my_dept_str +")" /*出錯
if @my_title_str is not null
          @result = @result + " and emp_title in ("+@my_title_str+")" /*出錯
if @my_emp_sn_str is not null
        @result = @result + " and emp_sn in ("+@my_emp_sn_str+")" /*出錯
execute @result
end
請問各位,為什麼會出錯。錯誤提示如下:Incorrect syntax near '@result'.
為什麼,請各位指點。多謝!
3#
发表于 2002-10-28 18:36:00 | 只看该作者
瞧着就眼晕。
假如你的三个if其中有一个条件成立,那么构造出来的东东应该是这样的:

select * from uv_wage_month_total_union_history where wage_date_year="+str(@my_year)+" and wage_date_month="+ str(@my_month) ?and ...

注意那个大问号,那是俺为了说明问题加进去的,问号前是变量,那么问号处是不是缺点什么?
    这只是就事论事了,可以把你构造的过程在VBA中进行测试,看看结果字符串是不是你想要的。或者存储过程什么也不干只返回一个字符串看看是个什么东东。
4#
 楼主| 发表于 2002-10-28 18:42:00 | 只看该作者
多些我師,我現在已找到原因了。在構造的過程中,變量和字構串之間的問題。
現更正如下:
create procedure usp_qry_xwage_xmonth_en @my_year smallint,@my_month tinyint,@my_dept_str  varchar(200),@my_title_str varchar(200),@my_emp_sn_str varchar(200)
as
begin
-- 多條件模糊查詢作者黃冠 email:hghuanguan@cmmail.com mobile:13005868658
declare @query as varchar(600)
set @query = 'select * from uv_wage_month_total_union_history where wage_date_year="'+str(@my_year)+'" and wage_date_month="'+ str(@my_month)
if @my_dept_str is not  null
         set @query = @query + '"  and dept_no in ("'+@my_dept_str +'")"'
if @my_title_str is not null
          set @query = @query + '" and emp_title in ("'+@my_title_str+'")"'
if @my_emp_sn_str is not null
           set @query = @query + '" and emp_sn in ("'+@my_emp_sn_str+'")"'
execute @query
end
----------------------------------
原因就在於:'"之間的區別,請老師講一下,多謝!
5#
 楼主| 发表于 2002-10-28 21:00:00 | 只看该作者
set @query = 'select * from uv_wage_month_total_union_history where wage_date_year="'+ str(@my_year)+'" and wage_date_month="'+ str(@my_month)
請問在這句中,因字段wage_date_year和wage_date_month是數字,但怎麼才可以再把上述字符串中的str(@my_year)和str(@my_month)轉換為數字哪?
多謝!
6#
发表于 2002-10-29 02:06:00 | 只看该作者
set @query = 'select * from uv_wage_month_total_union_history where wage_date_year= '+ str(@my_year)+' and wage_date_month='+ str(@my_month)
-- ??去掉引号是数字


[此贴子已经被作者于2002-10-28 18:06:11编辑过]

7#
 楼主| 发表于 2002-10-29 02:23:00 | 只看该作者
在這條語句中,如果有字符和數字是不可以用“+”號來連接的,我只有先轉為字符才可構造句,但在語句構造完成後又怎樣還原數字類型哪?有想過嗎?
8#
发表于 2002-10-29 02:49:00 | 只看该作者
我想@query 参数是文本,
set @query = 'select * from uv_wage_month_total_union_history where wage_date_year="'+ str(@my_year)+'" and wage_date_month="'+ str(@my_month)
假设 @QUERY 在完成后应该是
set @query = 'select * from uv_wage_month_total_union_history where wage_date_year= 2001 and wage_date_month= 2'
这样的形式,那么是否只要去除双引号 " 即可?

另外我想问一下, STR 为转换函数,那么 CAST 逆转换哪?
[此贴子已经被作者于2002-10-28 18:49:04编辑过]

9#
 楼主| 发表于 2002-10-29 02:58:00 | 只看该作者
但是現在的問題是您在轉換為數字時,且在構造時引入此函數,便無法構造,放下先不談,先看這個
set @query = 'select * from ufn_qry_xwage_xmonth_en('@my_year','@my_month')'
在這個中,怎樣把@my_year和@my_month變量,引入到函數中去哪?
且在函數中,多個參數間的逗句,譔怎麼處理哪?
10#
发表于 2002-10-29 03:42:00 | 只看该作者
set @query = 'select * from uv_wage_month_total_union_history where wage_date_year= ' + str(@my_year) + ' and wage_date_month = ' + str(@my_month)

我想问一下,这样可否运行?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|站长邮箱|小黑屋|手机版|Office中国/Access中国 ( 粤ICP备10043721号-1 )  

GMT+8, 2024-5-15 13:30 , Processed in 0.084242 second(s), 33 queries .

Powered by Discuz! X3.3

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表