This occasional use of T-SQL queries

Source: Internet
Author: User

Or write it by yourself.CodeComfortable to use

Declare @ SQL varchar (3000), @ cname varchar (100), @ ctype varchar (20), @ tablename varchar (50), @ condition varchar (200)
--------------------------------------------------------------------

Set @ tablename = 'table _ 1' -- table name
Set @ condition = 'where createdate> = '+ char (39) + '192. 100' + char (39) -- Condition

--------------------------------------------------------------------
Set @ SQL = 'select'
Declare column_name cursor
Select column_name, data_type from information_schema.columns where table_name = @ tablename order by ordinal_position
Open column_name
Fetch column_name into @ cname, @ ctype
While @ fetch_status = 0
Begin
If @ ctype = 'varchar 'or @ ctype = 'nvarchar' or @ ctype = 'Char 'or @ ctype = 'text'
Set @ SQL = @ SQL + 'Char (39) + '+ @ cname +' + char (39) '+ @ cname + ','
Else if @ ctype = 'datetime'
Set @ SQL = @ SQL + char (39) + 'getdate () '+ char (39) + ''+ @ cname + ','
Else
Set @ SQL = @ SQL + @ cname + ','

Fetch column_name into @ cname, @ ctype
End
Set @ SQL = @ SQL + '1 from' + @ tablename + @ Condition
Print @ SQL
Exec (@ SQL)
Close column_name
Deallocate column_name

 

Amount of data, etc.

Simple code Query

Declare @ tablename varchar (100), @ tablename2 varchar (50)
Create Table # xsage (sumcount int, tablename varchar (50 ))
Declare tablename_xsage cursor
Select table_name from information_schema.tables
Where table_type = 'base table'
Open tablename_xsage
Fetch tablename_xsage into @ tablename
While @ fetch_status = 0
Begin
Set @ tablename2 = char (39) + @ tablename + char (39)
Insert into # xsage
Exec ('select count (*), '+ @ tablename2 +' from' + @ tablename)
Fetch tablename_xsage into @ tablename
End
Close tablename_xsage
Deallocate tablename_xsage
Select sumcount, tablename from # xsage
Select sum (sumcount) from # xsage
Drop table # xsage

 

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.