-- In view of the fact that the field name is often required to obtain the list data of a data table. The following code is compiled.
-- Applicable scenarios: design or update select statements or insert statements for SQL stored procedures
-- The returned string format is similar to: "ID, begintime, endtime, addip, updateuser, updatetime, updateip"
-- Select name from syscolumns where id = object_id ('data table name ')
-- Define a data table name
Declare @ targettablename nvarchar (250)
Set @ targettablename = 'data table name'
-- Define the total number of Columns
Declare @ totlecolumns int
Select @ totlecolumns = count (*) from syscolumns where id = object_id (@ targettablename)
-- Print @ totlecolumns
-- Select * From syscolumns where id = object_id (@ targettablename)
-- Define all field name strings
Declare @ columnsstring nvarchar (4000)
Declare @ tempname nvarchar (4000)
Set @ columnsstring =''
Set @ tempname =''
Declare @ I int
Set @ I = 1
While @ I <= @ totlecolumns
Begin
-- Print @ I
Select @ tempname = Name from syscolumns where id = object_id (@ targettablename) and colorder = @ I
Set @ columnsstring = @ columnsstring + ',' + @ tempname
Set @ I = @ I + 1
End
If left (@ columnsstring, 1) = ', 'set @ columnsstring = substring (@ columnsstring, 2, Len (@ columnsstring)-1)
Print @ columnsstring