SQL notes [long-term update] (-2015.4)

Source: Internet
Author: User

"Traverse all tables, copy table structure, copy table data"

--INSERT statement
SELECT into from B is in the absence of a table, directly through the B table to create and copy the B table data into a table, and then the structure and data of the A/b table is exactly the same.
into Select from b is in the case of a table has been created, the B table data copied to a table, before the structure of a table is the same as the B table, or inserted when the error.

--Copy the table structure to the new library
SELECT into from DBTest.dbo.table1



--Query all table names in the library
SELECT from WHERE xtype=' U '
--SELECTfromWHERE type=' U '



--traverse all tables in the library and execute the specified SQL for all tables in bulk
Declare@tb_name nvarchar (100)
Declare@iint
Declare@tbTable(IDint Identity(+), name nvarchar (1000))

Set@i = 1
Set@tb_name =' CX '--All tables starting with this value

Insert into@tb (name)
SelectT.name
fromsysobjects T
whereXtype=' u ' andT.name like@tb_name +'% '

while@i <= (Select COUNT(*) from@tb)
begin
Select@tb_name = Name from@tbwhereid = @i;

exec(' select * from '[Email Protected]_name];

Set@i = @i + 1
End

SQL notes [long-term update] (-2015.4)

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.