Sp_executesql usage memo

Source: Internet
Author: User
Construct a dynamic SQL statement to obtain the return value from the cursor.

 

Declare @ tablename varchar (200)

Set @ tablename = 'tmpbasecustomer _ 9'

declare @ SQL nvarchar (4000)
set @ SQL = n'
declare @ repeatstr varchar (1000)
declare @ clname varchar (200)
declare @ total int
set @ repeatstr = '''
declare curcustomer cursor for
select name, count (name) as total from '+ quotename (@ tablename) + 'group by name having (count (*)> 1
open curcustomer
fetch next from curcustomer into @ clname, @ total
while @ fetch_status = 0
begin
If (@ total> 1)
begin
set @ repeatstr = @ repeatstr + '','' + @ clname + ''has ['' + Cast (@ total as varchar) + ''] line''
end
fetch next from curcustomer into @ clname, @ total
end
close curcustomer
deallocate curcustomer
select @ nrepeatstr = @ repeatstr
'
declare @ nrepeatstr varchar (1000)

Exec sp_executesql @ SQL,
N' @ nrepeatstr varchar (1000) out ',
@ Nrepeatstr out

Select @ nrepeatstr

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.