常用SQL SERVER 编程 技巧

有个表估计是存储过程插进去的吧! 所以想找到这个过程名.该如何找呢?
select object_Name(ID) from syscomments where text like '%表名%'

删除重复的ID留下时间最后的一条记录
IDNameLastLoginDate
001A2008-12-01
002B2008-12-03
001A2008-12-04
留下 001A2008-12-04
删除 001A2008-12-01

也就是ID 重复的 把最后的LastLoginDate 留下 其他的给删除

SQL code delete tb from tb t where LastLoginDate not in (select max(LastLoginDate) from tb where id = t.id)

DECLARE @vendor_id int, @vendor_name nvarchar(50), --嵌套双层游标循环
@message varchar(80), @product nvarchar(50)

PRINT '-------- Vendor Products Report --------'

DECLARE vendor_cursor<wbr style="LINE-HEIGHT: 1.3em"><font style="LINE-HEIGHT: 1.3em" color="#ffffff">CURSOR</font><wbr style="LINE-HEIGHT: 1.3em"> FOR--定义游标<br> SELECT VendorID, Name FROM Purchasing.Vendor WHERE PreferredVendorStatus = 1 ORDER BY VendorID<br><br>OPEN vendor_<font style="LINE-HEIGHT: 1.3em" color="#ffffff">cursor --打开游标</font><wbr style="LINE-HEIGHT: 1.3em"><br><br>FETCH NEXT FROM vendor_<font style="LINE-HEIGHT: 1.3em" color="#ffffff">cursor</font><wbr style="LINE-HEIGHT: 1.3em">INTO @vendor_id, @vendor_name 提取当前行的值到变量中<br><br>WHILE @@FETCH_STATUS = 0--如果有记录行则循环<br>BEGIN<br> PRINT ' '<br> SELECT @message = '----- Products From Vendor: ' + @vendor_name<br> PRINT @message<br><br> -- 要在内部定义内循环游标<br><br>DECLARE product_<font style="LINE-HEIGHT: 1.3em" color="#ffffff">cursor</font><wbr style="LINE-HEIGHT: 1.3em"><font style="LINE-HEIGHT: 1.3em" color="#ffffff">CURSOR</font><wbr style="LINE-HEIGHT: 1.3em"> FOR <br>SELECT v.Name FROM Purchasing.ProductVendor pv, Production.Product v <br>WHERE pv.ProductID = v.ProductID AND pv.VendorID = @vendor_id--游标带参数的是从外部获取值<br><br>OPEN product_<font style="LINE-HEIGHT: 1.3em" color="#ffffff">cursor</font><wbr style="LINE-HEIGHT: 1.3em"><br><br>FETCH NEXT FROM product_<font style="LINE-HEIGHT: 1.3em" color="#ffffff">cursor</font><wbr style="LINE-HEIGHT: 1.3em"> INTO @product<br>IF @@FETCH_STATUS &lt;&gt; 0 <br>PRINT ' &lt;&lt;None&gt;&gt;' <br>WHILE @@FETCH_STATUS = 0<br>BEGIN<br>SELECT @message = ' ' + @product<br>PRINT @message<br>FETCH NEXT FROM product_<font style="LINE-HEIGHT: 1.3em" color="#ffffff">cursor</font><wbr style="LINE-HEIGHT: 1.3em"> INTO @product<br>END<br>CLOSE product_<font style="LINE-HEIGHT: 1.3em" color="#ffffff">cursor</font><wbr style="LINE-HEIGHT: 1.3em"><br>DEALLOCATE product_<font style="LINE-HEIGHT: 1.3em" color="#ffffff">cursor</font><wbr style="LINE-HEIGHT: 1.3em"><br>FETCH NEXT FROM vendor_<font style="LINE-HEIGHT: 1.3em" color="#ffffff">cursor</font><wbr style="LINE-HEIGHT: 1.3em">INTO @vendor_id, @vendor_name -- 得到下个供应数据<br>END <br>CLOSE vendor_<font style="LINE-HEIGHT: 1.3em" color="#ffffff">cursor</font><wbr style="LINE-HEIGHT: 1.3em"></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

你可能感兴趣的:(sql,sql,编程,server)