Query statements of all tables in the database

Source: Internet
Author: User
Statement [SQL] declare @ tablenamenchar (20) declare @ fieldnamenchar (50) declare @ SQLStringnvarchar (300) Statements for querying all tables in the database -- declare cursor; -- game

Statement [SQL] declare @ tablename nchar (20) declare @ fieldname nchar (50) declare @ SQLString nvarchar (300) DECLARE tnames_cursor cursor local FORWARD_ONLY READ_ONLY -- DECLARE the cursor for select name from sysobjects where type = U; -- game

Query statements of all tables in the database

[SQL]

Declare @ tablename nchar (20)

Declare @ fieldname nchar (50)

Declare @ SQLString nvarchar (300)

DECLARE tnames_cursor cursor local FORWARD_ONLY READ_ONLY -- DECLARE the CURSOR

FOR select name from sysobjects where type = 'U'; -- column required FOR cursor

Open tnames_cursor -- open the cursor

Fetch next from tnames_cursor INTO @ tablename -- move the cursor to assign values to the variable. It should correspond to the columns required by the cursor, and the sequence type should be consistent.

WHILE (@ FETCH_STATUS = 0) -- loop

BEGIN

BEGIN

Set @ fieldname = 'Count' + @ tablename

SET @ SQLString = n' select count (*) as '+ @ fieldname + 'from' + @ tablename + 'having count (*)> 100 ';

EXECUTE dbo. sp_executesql @ SQLString

END

Fetch next from tnames_cursor INTO @ tablename -- move the cursor to assign a value to the variable which corresponds to the columns required by the cursor. The sequence type should be consistent.

END

CLOSE tnames_cursor -- CLOSE the cursor

DEALLOCATE tnames_cursor -- release cursor

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.