是的!是动态创建一个字符串!例如我要做一个修改某个表的值的时候用:
update A
set A1=@A1,A2=@A2,A3=@A3
这样一个例子!我如何通过动态的方式,根据传递不同的东西来更新不同情况呢?如我只修改A1和A2
那么我就要
update A
set A1=@A1,A2=@A2
同样道理其它都是根据传递的@A1,@A2,@A3的值来判断怎么动态写这个update语句!
update A
set A1=@A1,A2=@A2,A3=@A3
这样一个例子!我如何通过动态的方式,根据传递不同的东西来更新不同情况呢?如我只修改A1和A2
那么我就要
update A
set A1=@A1,A2=@A2
同样道理其它都是根据传递的@A1,@A2,@A3的值来判断怎么动态写这个update语句!
解决方案 »
- NOT EXISTS 嵌套 看不懂...
- sql创建存储过程
- sql server 如何查询两个没有关联的表。
- 高分!!!救我,用户 'NT AUTHORITY\NETWORK SERVICE' 登录失败 asp.net +ms sql 2000
- 解决MySQL的安装的问题
- 就只这么多分了。求助SQL SERVER中字型类型的具体含意是什么??
- sql server 2005
- 再次送分100问题,在线等待...
- 如何用VISIO描述表的关联?在线等待
- 此语句错在何处?
- 求助:由于卸载sqlServer2000没有把共享文件删去,没有办法在装sql2000了怎么办??
- sqlserver 如何保存使两图形?????
写存储过程
create procedure my(@aaa varchar(800))
as
begin
declare @bbb int
declare @id int
declare @sql varchar(8000)
declare @ccc varchar
select @sql='update A '
select @id=0;
select @bbb=-1;
while (@bbb<>0)
begin
set @bbb=charindex(',',@aaa)
select @id=@id+1;
if (@bbb=0)
begin
select @ccc=@aaa
end
else
begin
select @ccc=substring(@aaa,1,@bbb-1)
select @aaa=substring(@aaa,@bbb+1,len(@aaa)-@bbb)
end select @sql=@sql+' set A'+cast(@id as varchar)+'='''+@ccc+''','
end
select @sql=substring(@sql,1,len(@sql)-1)
print(@sql)
--exec(@sql)
end
end
alter procedure my(@aaa varchar(800))
as
begin
declare @bbb int
declare @id int
declare @sql varchar(8000)
declare @ccc varchar
select @sql='update A set '
select @id=0;
select @bbb=-1;
while (@bbb<>0)
begin
set @bbb=charindex(',',@aaa)
select @id=@id+1;
if (@bbb=0)
begin
select @ccc=@aaa
end
else
begin
select @ccc=substring(@aaa,1,@bbb-1)
select @aaa=substring(@aaa,@bbb+1,len(@aaa)-@bbb)
end select @sql=@sql+' A'+cast(@id as varchar)+'='''+@ccc+''','
end
select @sql=substring(@sql,1,len(@sql)-1)
exec(@sql)
end
当执行exec lst 'a,b'时实际执行为update A set a1='a',a2='b'
你自己再加约束条件吧,我没有仔细看你的哪一大堆代码。太长了。看着晕
--更新DO表数据
CREATE PROCEDURE UpdateDOItem
@UEdit Bit=0, --是否是修改:1为修改,0为添加
@Item_ID int=-1, --做的项目ID
@D_SUM_C int=-1, --项目数量
@D_SUM_M smallmoney=-1, --所需要的金额
@EM_ID1 int=-1, --第1个服务员ID
@EM_ID2 int=-1, --第2个服务员ID
@EM_ID3 int=-1, --第3个服务员ID
@Unit nvarchar(20)='', --项目单位
@OP_ID int=-1, --操作员ID
@Rom_ID int=-1, --房间ID
@Key_ID int=-1, --钥匙ID
@DO_ID int=-1, --要修改的项目标识
@Dan_ID int=-1 --添加的单据编号
AS
declare @strSQL nvarchar(600) -- 主语句
if @UEdit=1
begin
select @strSQL=
case @Item_ID when -1 then '' else ',Item_ID=@Item_ID ' end
+case @D_SUM_C when -1 then '' else ',D_SUM_C=@D_SUM_C' end
+case @D_SUM_M when -1 then '' else ',D_SUM_M=@D_SUM_M' end
+case @EM_ID1 when -1 then '' else ',EM_ID1=@EM_ID1' end
+case @EM_ID2 when -1 then '' else ',EM_ID2=@EM_ID2' end
+case @EM_ID3 when -1 then '' else ',EM_ID3=@EM_ID3' end
+case @Unit when '' then '' else ',Unit=@Unit' end
+case @OP_ID when -1 then '' else ',OP_ID=@OP_ID' end
+case @Rom_ID when -1 then '' else ',Rom_ID=@Rom_ID' end
+case @Key_ID when -1 then '' else ',Key_ID=@Key_ID' end
+',op_t=(getdate())'
+case @DO_ID when -1 then '' else ' where DO_ID=@DO_ID' end
,@strSQL=case @strSQL when '' then '' else 'update Do set '+stuff(@strSQL,1,1,'') endendelse
beginselect @strSQL=
case @DAN_ID when -1 then 'NULL' else '@DAN_ID' end
+case @Item_ID when -1 then ',NULL' else ',@Item_ID ' end
+case @D_SUM_C when -1 then ',NULL' else ',@D_SUM_C' end
+case @D_SUM_M when -1 then ',NULL' else ',@D_SUM_M' end
+case @EM_ID1 when -1 then ',NULL' else ',@EM_ID1' end
+case @EM_ID2 when -1 then ',NULL' else ',@EM_ID2' end
+case @EM_ID3 when -1 then ',NULL' else ',@EM_ID3' end
+',NULL,NULL,NULL'
+case @Unit when '' then ',NULL' else ',@Unit' end
+',getdate()'
+case @OP_ID when -1 then ',NULL' else ',@OP_ID' end
+case @Rom_ID when -1 then ',NULL' else ',@Rom_ID' end
+case @Key_ID when -1 then ',NULL' else ',@Key_ID' end
,@strSQL=case @strSQL when '' then '' else 'insert Do Values ('+@strSQL +')'endendselect @strSQL
if @strSQL != ''
exec sp_executesql @strSQL
,N'@UEdit Bit,
@Item_ID int,
@D_SUM_C int,
@D_SUM_M smallmoney,
@EM_ID1 int,
@EM_ID2 int,
@EM_ID3 int,
@Unit nvarchar(20),
@OP_ID int,
@Rom_ID int,
@Key_ID int,
@DO_ID int,
@Dan_ID int'
,@UEdit,@Item_ID,@D_SUM_C,@D_SUM_M,@EM_ID1,@EM_ID2,@EM_ID3,@Unit,@OP_ID,@Rom_ID,@Key_ID,@DO_ID,@Dan_ID
select @strSQL
GO