Common SQL Server programming skills

Source: Internet
Author: User

It is estimated that a table is inserted in the stored procedure! So how can I find the process name?
Select object_name (ID) from syscomments where text like '% table name %'

 

 

Deletes the record with the last time record of the repeat ID.
ID name lastlogindate
001 A 2008-12-01
002 B 2008-12-03
001 A 2008-12-04
001
Delete 001

That is, if the ID is repeated, the last lastlogindate is left for deletion.

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), -- nested double-layer cursor Loop
@ Message varchar (80), @ Product nvarchar (50)

Print '-------- vendor products report --------'

Declare vendor_cursorCursorFor -- Define a cursor
Select vendorid, name from purchasing. Vendor where preferredvendorstatus = 1 order by vendorid

Open vendor_cursor -- open the cursor

Fetch next from vendor_cursorInto @ vendor_id, @ vendor_name extract the value of the current row to the variable

While @ fetch_status = 0 -- loop if a record row exists
Begin
Print''
Select @ message = '----- products from Vendor:' + @ vendor_name
Print @ message

-- The internal cyclic cursor must be defined internally.

Declare product_cursorCursorFor
Select v. name from purchasing. productvendor PV, production. Product v
Where pv. productid = V. productid and PV. vendorid = @ vendor_id -- the cursor takes the parameter to obtain the value from the outside.

Open product_cursor

fetch next from product_cursor into @ Product
If @ fetch_status <> 0
Print ' '
while @ fetch_status = 0
begin
select @ message = ''+ @ Product
Print @ message
fetch next from product_cursor into @ Product
end
close product_cursor
deallocate product_cursor
fetch next from vendor_cursor into @ vendor_id, @ vendor_name -- get the next supply data
end
close vendor_cursor

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.