現在在用sqlserver和mysql,兩個數據庫的語法還不完全一樣,用的時候還得百度,麻煩,遂記錄一下:
sqlserver:
多表update:
update a set a.ssglid=b.id from xs_ssgl a,jc_ssgl b where a.ssglid=0 and a.xq=b.xq
存儲過程:
變量賦值:
select top 1 @ssglId=id,@xq=xq from jc_ssgl
判斷:
if @pSfyjsbf = '' or @sfyjsbf = @pSfyjsbf
begin
set @i = 1;
end
else
begin
set @i = 1;
end
遊標:
DECLARE cursor_jhk CURSOR FOR select id,xbdm from xs_ssgl where bj is not null and bj != '' and nf = @pNf;
OPEN cursor_jhk
FETCH NEXT FROM cursor_jhk INTO @id,@xbdm
WHILE @@FETCH_STATUS=0
BEGIN
FETCH NEXT FROM cursor_jhk INTO @id,@xbdm
END
CLOSE cursor_jhk
DEALLOCATE cursor_jhk
踩過的坑:
-- 外層會有遊標循環
set @ssglId=null;-- 此處不加初始化,下面查詢條件中的如果查不到,那麼@ssglId將使用上一次的數據,很奇怪sqlserver爲什麼沒有把空值賦給它,坑了近一個小時
select top 1 @ssglId=id,@xq=xq from jc_ssgl where ID not in (
select ssglId from xs_ssgl where nf=@pNf);
if @ssglId is not null
begin
-- 更新
update xs_ssgl set ssglid=@ssglId,xq=@xq where id=@id;
end
mysql:
多表update:
UPDATE aaa a, departments b SET d=b.id WHERE a.c=b.department