Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Export to CSV from results of a stored procedure

Author  Topic 

Menorel
Starting Member

15 Posts

Posted - 2013-07-22 : 15:58:50
Below is the sticking point of the Stored procedure. I dump my results of a pretty lengthy SP into a table (#tbl_ADPExport) and when I do a select all agianst this table it displays the results as I need them to. But as soon as I try to use sqlcmd to export the results the table I get this error:

Msg 102, Level 15, State 1, Procedure usp_PayRollTotalsExport, Line 156
Incorrect syntax near 'sqlcmd'.


Below is the Code I ma using in the SP what am I missing?




CREATE TABLE #tbl_ADPExport(
[Co Code] Varchar(50),
[Batch ID] Varchar(50),
[File #] Varchar(50),
[Pay #] Varchar(50),
[Job Cost#] Varchar(50),
[Tax Frequency] Varchar(50),
[Special Action] Varchar(50),
TempDept varchar(50),
[Rate Code] Varchar(50),
Shift Varchar(50),
[Temp Rate] Varchar(50),
[Reg Hours] Varchar(50),
[O/T Hours] Varchar(50),
[PTO Code] Varchar(50),
[PTO Hours] Varchar(50),
[HOL Code] Varchar(50),
[HOL Hours] Varchar(50),
[JD Code] Varchar(50),
[JD Hours] Varchar(50))
INSERT INTO #tbl_ADPExport
([Co Code], [Batch ID], [File #], [Pay #], [Job Cost#], [Tax Frequency], [Special Action], TempDept, [Rate Code], Shift, [Temp Rate],
[Reg Hours], [O/T Hours], [PTO Code],[PTO Hours], [HOL Code], [HOL Hours],[JD Code], [JD Hours])
SELECT DISTINCT
N'UR6' As [Co Code],
N'EPIP300' As [Batch ID],
#tbl_WORKEDPayRoll.EmployeeID As [File #],
N'' As [Pay #],
N'' As [Job Cost#],
N'' AS [Tax Frequency],
N'' As [Special Action],
#tbl_WORKEDPayRoll.TempDept,
N'' As [Rate Code],
N'' As Shift, N'' As [Temp Rate],
CASE WHEN SUM(#tbl_WORKEDPayRoll.Worked)>40.0 THEN 40.0 ELSE SUM(#tbl_WORKEDPayRoll.Worked) END AS [Reg Hours],
CASE WHEN SUM(#tbl_WORKEDPayRoll.Worked)<=40.0 THEN 0 ELSE SUM(#tbl_WORKEDPayRoll.Worked)- 40.0 END As [O/T Hours],
ISNULL(CASE WHEN #tbl_PTOPayRoll.Worked>0 THEN N'PTO' ELSE Null END, N'') As [PTO Code],
CASE WHEN #tbl_PTOPayRoll.Worked IS NULL THEN 0 ELSE SUM(#tbl_PTOPayRoll.Worked) END AS [PTO Hours],
ISNULL(CASE WHEN #tbl_HOLPayRoll.Worked>0 THEN N'HOL' ELSE NULL END, N'') As [HOL Code],
ISNULL(CASE WHEN #tbl_HOLPayRoll.Worked>0 THEN #tbl_HOLPayRoll.Worked ELSE 0 END, 0) AS [HOL Hours],
ISNULL(CASE WHEN #tbl_JDPayRoll.Worked>0 THEN N'JD' ELSE NULL END, N'') As [JD Code],
ISNULL(CASE WHEN #tbl_JDPayRoll.Worked>0 THEN #tbl_JDPayRoll.Worked ELSE 0 END, 0) AS [JD Hours]
FROM #tbl_HOLPayRoll RIGHT OUTER JOIN
#tbl_PTOPayRoll RIGHT OUTER JOIN
#tbl_WORKEDPayRoll ON #tbl_PTOPayRoll.EmployeeID = #tbl_WORKEDPayRoll.EmployeeID ON
#tbl_HOLPayRoll.EmployeeID = #tbl_PTOPayRoll.EmployeeID LEFT OUTER JOIN
#tbl_JDPayRoll ON #tbl_HOLPayRoll.EmployeeID = #tbl_JDPayRoll.EmployeeID
GROUP BY #tbl_WORKEDPayRoll.EmployeeID, #tbl_WORKEDPayRoll.TempDept, #tbl_WORKEDPayRoll.ActivityType, #tbl_PTOPayRoll.Worked, #tbl_HOLPayRoll.Worked, #tbl_JDPayRoll.Worked



--SELECT * FROM #tbl_ADPExport

sqlcmd -S . -d dbo.testdb -U someusername -P somepassword -s, -W -Q "Select * FROM #tbl_ADPExport" > \\servername\SharePathRoot\subfolder\ADPExport.csv

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2013-07-22 : 16:03:34
DECLARE @SQL NVARCHAR(MAX)

SET @SQL = 'sqlcmd -S . -d dbo.testdb -U someusername -P somepassword -s, -W -Q "Select * FROM #tbl_ADPExport" > \\servername\SharePathRoot\subfolder\ADPExport.csv';

exec xp_cmdshell @SQL



Microsoft SQL Server MVP, MCT, MCSE, MCSA, MCP, MCITP, MCTS, MCDBA
Go to Top of Page

Menorel
Starting Member

15 Posts

Posted - 2013-07-22 : 16:48:56
SwePeso - got that working and the a file is being exported to the location I am wanting but the ony data in it is this error message and no data from the temp table. The variable is declared and set at the begining of the SP and the executed after the '#tbl_ADPExport' has been populated in the SP so not sure why it isn't finding it. If I keep the Server Info in the sqlcmd then I still get the same error message in the exported file.

Msg 208, Level 16, State 1, Server SCFS, Line 1
Invalid object name '#tbl_ADPExport'.

Here is the adapted SQLCMD i am using to get it working:

DECLARE @SQL NVARCHAR(1000)
SET @SQL = 'sqlcmd -s, -W -Q "Select * FROM #tbl_ADPExport" > \\susagpwfm01\TRE_Files\PayRollExport\ADPExport.csv'
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2013-07-22 : 16:52:00
Use global temporary table ##tbl_ADPExport, instead of local temporary table #tbl_ADPExport.


Microsoft SQL Server MVP, MCT, MCSE, MCSA, MCP, MCITP, MCTS, MCDBA
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-07-23 : 01:20:52
Also be aware of problem that may happen using ## tables in concurrent execution

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
   

- Advertisement -