Create a stored procedure and return the primary key of the table by passing in the table name parameter. The Stored Procedure Code is as follows:
Create proc pgetpkcolumn
(@ Tablename varchar (50 ))
As
/*
Create by leleyl
Obtain the auto-incrementing column of the data table to be imported.
*/
Declare @ columnname varchar (500)
Set @ columnname =''
Select @ columnname = @ columnname + ',' + C. Name
From
Sysindexes I,
Sysindexkeys K,
Sysobjects O,
Syscolumns C,
Policypes d
Where
I. name like 'pk _ %'
And O. ID = I. ID
And O. xtype = 'U'
And I. ID = K. ID
And I. indid = K. indid
And I. ID = O. ID
And I. ID = C. ID
And K. colid = C. colid
And I. indid> 0
And I. indid <255
And (I. Status & 64) = 0
And C. xtype = D. xtype
And O. Name = @ tablename
If charindex (',', @ columnname) = 1
Select stuff (@ columnname, 1, 1 ,'')
Else
Select @ columnname