我在这里写了一些有关预期输出的内容,但它不适用于最后一列,所有字段都应以逗号结尾(不包括最后一列的值),我使用像这样的东西我的代码是波纹管
declare @query varchar set @Query = Left(@Query,len(@Query)-1)
SELECT '@' + COLUMN_NAME as c1,
CASE DATA_TYPE
WHEN 'NVARCHAR' then 'nvarchar(max)' + ','
WHEN 'INT' then 'int' + ','
WHEN 'DATETIME' then 'Datetime()' + ','
when 'Float' then 'float' + ','
end
as c2
from INFORMATION_SCHEMA.COLUMNS where TABLE_NAME='tbl_data'
c1 c2
@MastCode float,
@BALCO_EMP_ID float,
@MastName nvarchar(max),
@FatherName nvarchar(max),
@Department nvarchar(max),
@PlantName nvarchar(max),
@PO nvarchar(max),
@PO_START nvarchar(max),
@PO_END nvarchar(max),
@Contractor nvarchar(max),
@ContractorCode nvarchar(max)
答案 0 :(得分:1)
您可以将当前的查询逻辑放入CTE中,然后使用解析函数处理最终的逗号:
WITH cte AS (
SELECT '@' + COLUMN_NAME AS c1,
CASE DATA_TYPE
WHEN 'NVARCHAR' THEN 'nvarchar(max)'
WHEN 'INT' THEN 'int'
WHEN 'DATETIME' THEN 'Datetime()'
WHEN 'Float' THEN 'float' END AS c2,
ROW_NUMBER() OVER (ORDER BY DATA_TYPE) AS rn,
COUNT(*) OVER () AS cnt
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'tbl_data'
)
SELECT
c1 As col1,
c2 + CASE WHEN rn < cnt THEN ',' ELSE '' END AS col2
FROM cte;