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.