[转]sqlserver中,sql编程的几个小常识

[转]sqlserver中,sql编程的几个小常识

1、取出刚刚插入(删除)的数据SELECT 字段名 FROM INSERTED(DELETED)
2、对于UPDATE实际上是先DELETE然后再INSERT所以如果想得到UPDATE前后的数据值,应该先从DELETED取出,然后从INSERTED取出;
3、IF UPDATE(列名)可以判断更新或插入哪一个字段的值;
4、@@ROWCOUNT可以判断上一行查询操作得到的列数;
5、给变量赋值用SET @ZQB = 13;
6、察看是否有符合条件的记录IF EXISTS (SELECT name FROM sysobjects WHERE name = 'reminder' AND type = 'TR');
7、定义游标,如下:
DECLARE c1 CURSOR FOR
SELECT emp_mgr.emp
FROM emp_mgr, inserted
WHERE emp_mgr.emp = inserted.mgr

OPEN c1
FETCH NEXT FROM c1 INTO @e--从游标中取出数据
WHILE @@fetch_status = 0--判断是否到最后
BEGIN
UPDATE emp_mgr
SET emp_mgr.NoOfReports = emp_mgr.NoOfReports + 1 -- Add 1 for newly
WHERE emp_mgr.emp = @e -- added employee.

FETCH NEXT FROM c1 INTO @e
END
CLOSE c1
DEALLOCATE c1--删除游标引用

你可能感兴趣的:([转]sqlserver中,sql编程的几个小常识)