MsSql 游标 修改字段两个表关联 表向另个表插入记录

-- 方法1:游标 sqlserver
-- 声明变量
DECLARE
@SystemUserId AS UNIQUEIDENTIFIER

-- 声明游标
DECLARE C_SystemUser CURSOR FAST_FORWARD FOR
SELECT SystemUserId 
FROM Quotation.dbo.SystemUser WHERE SystemUserNo NOT IN ('beijing','dicai','admin','test') ;

OPEN C_SystemUser;

-- 取第一条记录
FETCH NEXT FROM C_SystemUser INTO @SystemUserId;

WHILE @@FETCH_STATUS=0
BEGIN
-- 操作

INSERT INTO Quotation.dbo.SystemUserRole(SystemUserId,SystemRoleId) VALUES(@SystemUserId,'549F845D-0F84-4422-9625-BFA2703288DD')
-- 取下一条记录
FETCH NEXT FROM C_SystemUser INTO @SystemUserId;
END

-- 关闭游标
CLOSE C_SystemUser;

-- 释放游标
DEALLOCATE C_SystemUser;

 

--修改字段两个表关联 sqlserver
UPDATE a SET a.Password=b.Password
FROM  a inner join  b on a.SystemUserNo=b.SystemUserNo 
where a.SystemUserNo!='admin'

mysql

update WlwMessage a inner join WlwEquipment b on a.RId =b.Id set a.DeviceName=b.EquipmentNum where a.RType=1

update table_name1 a
set a.name= (select b.name from table_name2 b where a.oid= b.id)
where exists (select 1 from table_name2 c where a.oid= c.id);

UPDATE sturgeon_summary s SET s.set_date = (SELECT set_date FROM sturgeon_set_pay p WHERE s.set_no = p.set_no ),
s.merchant_no = (SELECT merchant_no FROM sturgeon_set_pay p WHERE s.set_no = p.set_no ) WHERE s.set_no ="M2201904290100100010039093733702";


一个表向另一个表插入记录

假如a表存在,则
insert into A(a,b,c) select a,b,c from B

假如A表不存在,则
select a,b,c into A from B

 

--新增表字段

if not exists(select * from syscolumns where id=object_id('Provider') and name='Creater')
begin
alter table Provider add Creater uniqueidentifier null 
END

--添加给字段 默认值

ALTER TABLE [dbo].[QuotationFille] ADD CONSTRAINT [DF_QuotationFille_Status] DEFAULT ((0)) FOR [Status]

ALTER TABLE [dbo].[QuotationFille] ADD CONSTRAINT [DF_QuotationFille_CreatedOn] DEFAULT (getdate()) FOR [CreatedOn]

 

posted @ 2019-03-08 21:58  cclon  阅读(360)  评论(0编辑  收藏  举报