SQL Server method to add a field to multiple tables

Source: Internet
Author: User

1. Using Cursors

DECLARE @sql varchar ( $), @name varchar ( +) DECLARE my_cursor scroll cursor for    SelectName fromsysobjectswhereType ='u'and name like'jobs_%'Open My_cursorfetch Next frommy_cursor into @name while(@ @fetch_status =0) BEGIN Print'processing'+@nameSet@sql = N'ALTER TABLE Testbfcmisuser.'+ @name + N'Add note varchar ($)'exec (@sql) print'finished'FETCH Next frommy_cursor into @nameendclose my_cursordeallocate my_cursor

2. Using temporary tables

DECLARE @sql varchar ( $), @name varchar ( -), @countintSelectIdentityint,1,1) asID, name into #JobTable fromsysobjectswhereType ='u'and name like'jobs_%'Set@count =@ @rowcount while@count >0beginSelectTop1@name =name from#JobTablewhereID =@count Print'processing'+@nameSet@sql = N'ALTER TABLE Testbfcmisuser.'+ @name + N'Add note varchar ($)'exec (@sql) print'finished'        Set@count = @count-1Enddrop Table #JobTable


Http://www.wonima.com Oh, funny Web

SQL Server method to add a field to multiple tables

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.