这个存储过程怎么传值啊?

nitaiyoucala 2015-03-20 10:11:49

CREATE PROCEDURE [dbo].[Up_Pretreatment_registerNo]
@status nvarchar(50) output,
@客户ID_OutPut nvarchar(50) output,
@strSQL nvarchar(max)
AS

select t1.工商注册号 as 导入工商注册号,
t1.联系电话 as 导入电话,
t1.客户名称 as 导入客户名称,
t1.联系人 as 导入联系人,
t1.备注 as 导入备注,
t2.工商注册号 as 库内工商注册号,
t2.客户ID as 库内客户ID,
t3.ph as 库内电话,
case when t2.客户ID is not null and t3.ph is not null then 0 when t2.客户ID is not null and t3.ph is null then 1
when t2.客户ID is null then 2 end as 状态
from (
这里放@strSQL会报错?怎么解决啊?
) t1
left join pre_database t2 on t1.工商注册号=t2.工商注册号
left join pre_database_phone t3 on t2.客户ID=t3.客户ID and t3.ph=t1.联系电话
group by t1.工商注册号,t1.联系电话,t2.工商注册号,t3.ph,t2.客户ID,t1.客户名称,t1.联系人,t1.备注,
case when t2.客户ID is not null and t3.ph is not null then 0 when t2.客户ID is not null and t3.ph is null then 1
when t2.客户ID is null then 2 end


@strSQL 的值是:
select '310115000987594' 工商注册号,'021-63523522' 联系电话,'上海百度' 客户名称,'杜成刚' 联系人,'哈哈我是备注' 备注
union select '310115000987594' 工商注册号,'153434312101010' 联系电话,'腾讯' 客户名称,'李四' 联系人,'我啊啊' 备注
union select '310115000987593' 工商注册号,'021-5975505' 联系电话,'北京新浪' 客户名称,'张三' 联系人,'没有啊' 备注
union select '3100007432121233343' 工商注册号,'13886448928' 联系电话,'中软国际' 客户名称,'王五' 联系人,'呵呵' 备注
...全文
162 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
reenjie 2015-03-20
  • 打赏
  • 举报
回复
動態sql解決

CREATE PROCEDURE [dbo].[Up_Pretreatment_registerNo]
@status nvarchar(50) output,
@客户ID_OutPut nvarchar(50) output,
@strSQL nvarchar(max)
AS
 declare @sql nvarchar(max)
set @sql='select t1.工商注册号 as 导入工商注册号,
t1.联系电话 as 导入电话,
t1.客户名称 as 导入客户名称,
t1.联系人 as 导入联系人,
t1.备注 as 导入备注,
t2.工商注册号 as 库内工商注册号,
t2.客户ID as 库内客户ID,
t3.ph as 库内电话,
case when t2.客户ID is not null and t3.ph is not null then 0 when t2.客户ID is not null and t3.ph is null then 1 
when t2.客户ID is null then 2 end as 状态
from ('+@strSQL+'
) t1 
left join pre_database t2 on t1.工商注册号=t2.工商注册号 
left join pre_database_phone t3 on t2.客户ID=t3.客户ID and t3.ph=t1.联系电话 
group by t1.工商注册号,t1.联系电话,t2.工商注册号,t3.ph,t2.客户ID,t1.客户名称,t1.联系人,t1.备注,
case when t2.客户ID is not null and t3.ph is not null then 0 when t2.客户ID is not null and t3.ph is null then 1 
when t2.客户ID is null then 2 end'
exec(@sql)

  • 打赏
  • 举报
回复
用exec执行sql字符串
江南小鱼 2015-03-20
  • 打赏
  • 举报
回复
1、拼SQL,动态SQL,如2#所示 2、拼SQL有一个弊端,比如@strSQL已经灰常长了,拼凑的动态SQL赋值给@sql可能会超过变量允许的最大长度 考虑第二种办法: ①、先把@strSQL插入临时表

CREATE TABLE #TMP(//列定义)
INSERT INTO #TMP EXEC(@strSQL)
②、后面的查询使用#TMP

34,575

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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