Access to SQL Server user table information cursor

Source: Internet
Author: User
Tags definition datetime table name
Server/* Access to User table information travel target
SET NOCOUNT on
DECLARE find_user_table CURSOR
For
SELECT [name],crdate from sysobjects
WHERE type= ' U '/* Here only refer to the User Defined table * *
Order BY [name]

DECLARE @cName VARCHAR (128)/* Definition Storage table name change *
DECLARE @crdate DATETIME/* Definition table creation Date Change *
DECLARE @Rows INT/* Definition table row number Change *

CREATE TABLE #tmpTable/* Create a temporary schedule for storing information
(
TableName VARCHAR (128),
Crdate datetime,
Row INT
)
Open find_user_table//* Open the target * *
Fetch NEXT from find_user_table into @cName, @crdate/* Read the table name from the cursor to the variable *
While @ @FETCH_STATUS =0
BEGIN
DECLARE @cSql nvarchar (+), @par nvarchar (30)
SELECT @par = ' @nRows INT OUTPUT '
Select @cSql = ' Select @nRows =count (*) from [' + @cName + '] '
EXECUTE sp_executesql @cSql, @par, @rows OUTPUT/* Calculates the total number of the preceding table * *

INSERT into #tmpTable ValueS (@cName, @crdate, @rows)/* To save the information in the preceding table to a temporary form *
Fetch NEXT from find_user_table into @cName, @crdate/* Read the table name from the cursor to the variable *
End
SELECT * FROM #tmpTable/* Displays all table information * *
DROP Table #tmpTable/* Delete temporary table * *
Close Find_user_table/* Close the cursor/*
DEALLOCATE find_user_table/* Release the Tour target * *



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.