Obtain all SQL stored procedure scripts, including the SQL table name, view name, stored procedure name, and column name.

Source: Internet
Author: User

 

Some of the content collected on the Internet may be faulty. You need to analyze which ones are correct, but most of them are okay !.

[1] method:

You can query the Stored Procedure script:

Select * From syscomments

You can query the table name, view name, and stored procedure name.

Select * From sysobjects

-- Where xtype = 'U' -- table name

-- Where xtype = 'V' -- View name

-- Where xtype = 'pk' -- primary key

-- Where xtype = 'P' -- Stored Procedure

[2] method:

1. Get all database names: 

(1 ),SelectName
From master .. sysdatabases order by name
2.Get all table names:
(1 ),SelectName
From sysobjects where xtype = ''u'' order by name

Xtype = ''u'': indicates all user tables;

Xtype = 's': indicates all system tables;

(2 ),SelectName
From sysobjects where type = ''u'' and sysstat = ''83''

Note: Generally, only type is required.
= ''U'', but sometimes there will be system tables mixed in it (I don't know why), add the following sentence to delete these system tables.


3.Get all field names: 

(1 ),SelectName
From syscolumns where id = object_id (''tablename '')

(2 ),SelectSyscolumns. Name,Policypes. Name, Syscolumns. isnullable, syscolumns. Length
From syscolumns, policypes where syscolumns. xusertype = policypes. xusertype and "syscolumns. ID = object_id (''tablename '')

Note:

(A) In order to highlight some important content, several pieces of information are selected for output.

(B) The syscolumns table only contains data type numbers. To obtain the complete name, you need to find it from the categorypes table. Generally, it is better to use xusertype for user data types, there will be no one-to-many cases.

(C) syscolumns. length is the length of the physical memory, so nvarchar, varchar, and Other types are shown in the database as half of this.

4. Obtain the column names of the primary keys in the table:


Select
 Syscolumns. NameFrom
Syscolumns, sysobjects, sysindexes, sysindexkeys where syscolumns. id = object_id (''tablename'') and sysobjects. xtype = ''pk'' and sysobjects. parent_obj = syscolumns. ID and sysindexes. id = syscolumns. ID and sysobjects. name = sysindexes. name and sysindexkeys. ID
= Syscolumns. ID and sysindexkeys. indid = sysindexes. indid and syscolumns. colid = sysindexkeys. colid

Note: This is found in four system tables. The relationship is complex and can be roughly expressed:

Syscolumns contains the column information and table ID in the table. The sysobjects table contains the primary key name (similar to pk_table) and Table ID.
There are primary key names, table IDs, and index numbers. In sysindexkeys, there are table IDs, index numbers, and column numbers. After one item is matched, the column names can be found.

 

You can also use the following to obtain the stored procedure:

Exec sp_helptext @ objname = 'guestcomplaintsandnetcommentreport'

Exec sp_helptext 'guestcomplaintsandnetcommentreport'

Example of obtaining Stored Procedure content by name:

Select * From syscomments
Select * From sysobjects
Where xtype = 'P'

Select sobj. ID, sobj. Name, comment. Text
From syscomments as comment
Inner join sysobjects as sobj
On sobj. ID = comment. ID
Where sobj. xtype = 'P'
And comment. ID = '20140901'

To get a table column Name:

Select * From sysobjects
Where name = 'productstype'

Select * From syscolumns
Where id = '000000'

 

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.