Get the column information of the table, including the column name, column data type, primary key column.

xiaoxiao2021-03-06  64

The stored procedure, saved, ^ _ ^ create procedure dbo.trablename varchar (50) / * Obtain the information, column name, column data type, data type length of the corresponding table, according to the incoming table name Column is the primary key list * / as / * get the column name of the primary key column of the table, save in @CLNAME * / declare @SQL nVARchar (200) Declare @colname varchar (50)

set @ Sql = N'select @ ColName = name from syscolumns where exists (select id, indid from sysindexes where name = '' PK _ ' @ TableName ' '' and syscolumns.id = sysindexes.id and syscolumns.colid = sysindexes. Indid) 'EXEC SP_EXECUTESQL @ SQL, N' @ ColName Varchar (50) Out ', @ colname out

/ * Get the column information, column name, column data type, column data type length of the table, column is the primary key column * / select syscolumns.name as colname, systemus.name as collength, code syscolumns. name when @ColName then 1 else 0 end as IsPkColumnfrom syscolumns join sysobjects on syscolumns.id = sysobjects.id join systypes on systypes.xtype = syscolumns.xtypewhere sysobjects.name=@TableNameGO------------ ---------------------------- Simple acquisition of the primary key column name, so you can: sp_pkeys 'YOUBLE'

You can get the value of the primary key field you want.

转载请注明原文地址:https://www.9cbs.com/read-90341.html

New Post(0)