2015-02-10 22 views
-1

說我在表格和主鍵列中有未知的N列,所以我想要CSV格式的特定主鍵的所有列。選擇未知的列號爲csv

N可以是任意數量的列

ID Name Class RollNo Language State Country 

1 Aditya 12th 5  English C.A. USA 

2 XYZ  X  3  AA  AA  AAA 

輸出應該像

1,Aditya,12th,5,English,CA,USA 

2,XYZ,X,3,AA,AA,AAA 
在SQL SERVER

回答

0

試試這個

 DECLARE @sql  VARCHAR(8000), 
     @dbname  VARCHAR(50)='', 
     @schemaname VARCHAR(50)='dbo', 
     @tablename VARCHAR(50)='' 

SELECT @sql = 'bcp ' + @dbname + '.' + @schemaname + '.' 
       + @tablename 
       + ' out 
c:\bcp\tablename.csv -u username -p password -S' 
       + @@SERVERNAME 
EXEC master..xp_cmdshell 
    @sql 

--[Note:The path should be your server path] 
--TO configure xp_cmdshell 
EXEC sp_configure 
    'show advanced options', 
    1; 

GO 

-- To update the currently configured value for advanced options. 
RECONFIGURE; 

GO 

-- To enable the feature. 
EXEC sp_configure 
    'xp_cmdshell', 
    1; 

GO 

-- To update the currently configured value for this feature. 
RECONFIGURE; 

GO 
+0

我不想csv文件我只是希望它作爲行返回在SQL中 – 2015-02-10 05:56:42