sql替換問題

Dear SQL(燊) 2011-06-01 11:44:23
表1
SQLID ID VAL
1 1 1
1 2 ffwe
2 1 1
2 2 Red
表2
SQLID sSQL
1 select count(*) from venmasm where ven_id<>$1$ and ven_code=rtrim('$2$')
2 select count(*) from colorflm where color_id<>$1$ and color_code=rtrim('$2$')
大家好!幫忙解決以下問題
兩表關聯字段是SQLID
表1 中的ID是表2 sSQL中$$引用中的值,現將表1中的VAL替換到對應表2中sSQL'$$'中
要求不用函數和循環,最好能用1-2條SQL解決,謝謝!
...全文
167 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
Dear SQL(燊) 2011-06-10
  • 打赏
  • 举报
回复
大家來幫幫忙啊
cd731107 2011-06-09
  • 打赏
  • 举报
回复
明白了,表1必須是臨時表,參數是不確定,要求不用函數和循環
cd731107 2011-06-09
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 chenshanliang 的回复:]
引用 13 楼 cd731107 的回复:
SQL code
update 表2 set sSQL=replace(sSQL,'$1$',a.val)
from 表1 a where 表2.SQLID=a.SQLID and a.id=1

update 表2 set sSQL=replace(sSQL,'$2$',a.val)
from 表1 a where 表2.SQLID=a.S……
[/Quote]
能说下不行的原因吗?
Dear SQL(燊) 2011-06-09
  • 打赏
  • 举报
回复
加分求解
Dear SQL(燊) 2011-06-08
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 cd731107 的回复:]
SQL code
update 表2 set sSQL=replace(sSQL,'$1$',a.val)
from 表1 a where 表2.SQLID=a.SQLID and a.id=1

update 表2 set sSQL=replace(sSQL,'$2$',a.val)
from 表1 a where 表2.SQLID=a.SQLID and a.id=2
[/Quote]
這樣不行
cd731107 2011-06-08
  • 打赏
  • 举报
回复
update 表2 set sSQL=replace(sSQL,'$1$',a.val)
from 表1 a where 表2.SQLID=a.SQLID and a.id=1

update 表2 set sSQL=replace(sSQL,'$2$',a.val)
from 表1 a where 表2.SQLID=a.SQLID and a.id=2
Dear SQL(燊) 2011-06-08
  • 打赏
  • 举报
回复
現在函數都不能解決,因為我的表1必須是臨時表
will234079285 2011-06-08
  • 打赏
  • 举报
回复
同问!这个怎么弄啊
Dear SQL(燊) 2011-06-08
  • 打赏
  • 举报
回复
急!有沒有人知道?
Dear SQL(燊) 2011-06-01
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 jxqn_liu 的回复:]
你那个后面的参数如果是固定的还好办,不固定的话估计得用函数咯
下面是一个固定参数为2个的示例:

SQL code

declare @t1 table(SQLID int, ID int, VAL varchar(20))
declare @t2 table(SQLID int, sSQL varchar(2000))
insert into @t1
select 1, 1, '……
[/Quote]
不好意思,參數是不確定的如下:
SQLID ID VAL
1 1 1
1 2 ffwe
2 1 1
2 2 Red
2 3 df
表2
SQLID sSQL
1 select count(*) from venmasm where ven_id<>$1$ and ven_code=rtrim('$2$')
2 select count(*) from colorflm where color_id<>$1$ and color_code=rtrim('$2$') and clode_name='$3$'
jxqn_liu 2011-06-01
  • 打赏
  • 举报
回复
你那个后面的参数如果是固定的还好办,不固定的话估计得用函数咯
下面是一个固定参数为2个的示例:

declare @t1 table(SQLID int, ID int, VAL varchar(20))
declare @t2 table(SQLID int, sSQL varchar(2000))
insert into @t1
select 1, 1, '1' union all
select 1, 2, 'ffwe' union all
select 2, 1, '1' union all
select 2, 2, 'Red'

insert into @t2
select 1, 'select count(*) from venmasm where ven_id<>$1$ and ven_code=rtrim(''$2$'')' union all
select 2, 'select count(*) from colorflm where color_id<>$1$ and color_code=rtrim(''$2$'')'

select REPLACE(REPLACE(A.sSQL, '$1$', B.ID1), '$2$', B.ID2) SQL from @t2 A left join (
select SQLID, max(case when ID=1 then VAL else '' end) ID1
, max(case when ID=2 then VAL else '' end) ID2
from @t1 group by SQLID) B on A.SQLID=B.SQLID

/*
SQL
----------------------------------------------------------------------------
select count(*) from venmasm where ven_id<>1 and ven_code=rtrim('ffwe')
select count(*) from colorflm where color_id<>1 and color_code=rtrim('Red')
*/
Dear SQL(燊) 2011-06-01
  • 打赏
  • 举报
回复
替換後的結果
SQLID sSQL
1 select count(*) from venmasm where ven_id<>1 and ven_code=rtrim('ffwe')
2 select count(*) from colorflm where color_id<>2 and color_code=rtrim('Red')
--小F-- 2011-06-01
  • 打赏
  • 举报
回复
那写出你需要的结果
Dear SQL(燊) 2011-06-01
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 qianjin036a 的回复:]
第二条少了个 a

SQL code
select count(*) from venmasm a where ven_id<>(select top 1 val from 表1 where sqlid=a.sqlid and id=1) and ven_code=rtrim('$2$')
select count(*) from colorflm a where color_id<>(s……
[/Quote]
這樣不行啊,表2中不是一兩條記錄
Dear SQL(燊) 2011-06-01
  • 打赏
  • 举报
回复
id<>$1$中的“$1$”就是要替換用表1中對應的VAL
替換後如下:
SQLID sSQL
1 select count(*) from venmasm where ven_id<>1 and ven_code=rtrim('ffwe')
2 select count(*) from colorflm where color_id<>2 and color_code=rtrim('Red')
-晴天 2011-06-01
  • 打赏
  • 举报
回复
第二条少了个 a
select count(*) from venmasm a where ven_id<>(select top 1 val from 表1 where sqlid=a.sqlid and id=1) and ven_code=rtrim('$2$')
select count(*) from colorflm a where color_id<>(select top 1 val from 表1 where sqlid=a.sqlid and id=2) and color_code=rtrim('$2$')
-晴天 2011-06-01
  • 打赏
  • 举报
回复
是这个意思吗?
select count(*) from venmasm a where ven_id<>(select top 1 val from 表1 where sqlid=a.sqlid and id=1) and ven_code=rtrim('$2$')
select count(*) from colorflm where color_id<>(select top 1 val from 表1 where sqlid=a.sqlid and id=2) and color_code=rtrim('$2$')
-晴天 2011-06-01
  • 打赏
  • 举报
回复
这是什么语句?
id<>$1$
在MSSQL不能这么写的.

27,579

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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