SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Export to CSV from results of a stored procedure
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Menorel
Starting Member

USA
15 Posts

Posted - 07/22/2013 :  15:58:50  Show Profile  Reply with Quote
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

Sweden
30265 Posts

Posted - 07/22/2013 :  16:03:34  Show Profile  Visit SwePeso's Homepage  Reply with Quote
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

USA
15 Posts

Posted - 07/22/2013 :  16:48:56  Show Profile  Reply with Quote
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

Sweden
30265 Posts

Posted - 07/22/2013 :  16:52:00  Show Profile  Visit SwePeso's Homepage  Reply with Quote
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

India
52317 Posts

Posted - 07/23/2013 :  01:20:52  Show Profile  Reply with Quote
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
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.05 seconds. Powered By: Snitz Forums 2000