-
StackOverflow 文档
-
Microsoft SQL Server 教程
-
STUFF 功能
-
获取用逗号分隔的列名(不是列表)
/*
The result can be use for fast way to use columns on Insertion/Updates.
Works with tables and views.
Example: eTableColumns 'Customers'
ColumnNamesId, FName, LName, Email, PhoneNumber, PreferredContact
INSERT INTO Customers (Id, FName, LName, Email, PhoneNumber, PreferredContact)
VALUES (5, 'Ringo', 'Star', 'two@beatles.now', NULL, 'EMAIL')
*/
CREATE PROCEDURE eTableColumns (@Table VARCHAR(100))
AS
SELECT ColumnNames =
STUFF( (SELECT ', ' + c.name
FROM
sys.columns c
INNER JOIN
sys.types t ON c.user_type_id = t.user_type_id
WHERE
c.object_id = OBJECT_ID( @Table)
FOR XML PATH, TYPE).value('.[1]','varchar(2000)'),
1, 1, '')
GO