How to Export data from SQL Server to CSV using SQL server Management studio?

  • Hi All,

    Good morning,

    I have requirement export data from SQL server to CSV using SSMS, because we don't have access to execute SSIS package from management studio using XP_cmdshell , so can you please any one help me ? how to export the data without xp_cmdshell ?

    Thanks in Advance.

    Kanagarajan s.

  • One simple way would be the following steps:

    1. Run the SELECT query in your SSMS( SELECT * FROM YourTableName )

    2. In the Results tab, you can select all the grid cells together and save the results as a file with csv extension


    Kingston Dhasian

    How to post data/code on a forum to get the best help - Jeff Moden
    http://www.sqlservercentral.com/articles/Best+Practices/61537/

  • manavairajan (6/15/2013)


    I have requirement export data from SQL server to CSV using SSMS, because we don't have access to execute SSIS package from management studio using XP_cmdshell , so can you please any one help me ? how to export the data without xp_cmdshell ?

    I apologize in advance if this won't work for you because I did see that you are trying to do this without xp_cmdshell. But I have a procedure that's been hanging around since SQL2K. This can be run directly from SSMS and doesn't require SSIS at all. Perhaps, just maybe, this could help you or you could present it to the "powers that be" and get them to change their mind? You could also just run this locally and grab your query via a linked server so you wouldn't actually be running xp_cmdshell on a production machine. Depending on what you need this for that could get annoying after awhile, but maybe better to run it manually that way than not at all.

    Let me give you the code and some sample data so you could at least try it on your local server for testing purposes:

    Set up some sample data

    CREATE TABLE [dbo].[TestExport](

    [RowNum] [int] IDENTITY(1,1) NOT NULL,

    [SplitterName] [varchar](50) NULL,

    [NumberOfRows] [int] NULL,

    [NumberOfElements] [int] NULL,

    [MinElementLength] [int] NULL,

    [MaxElementLength] [int] NULL,

    [Duration] [decimal](9, 5) NULL,

    [MinLength] [int] NULL,

    [AvgLength] [int] NULL,

    [MaxLength] [int] NULL,

    [Run] [int] NULL,

    PRIMARY KEY CLUSTERED

    (

    [RowNum] ASC

    )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]

    ) ON [PRIMARY]

    GO

    SET IDENTITY_INSERT [dbo].[TestExport] ON

    INSERT [dbo].[TestExport] ([RowNum], [SplitterName], [NumberOfRows], [NumberOfElements], [MinElementLength], [MaxElementLength], [Duration], [MinLength], [AvgLength], [MaxLength], [Run]) VALUES (1, N'Split', 1000, 1, 1, 10, CAST(0.01300 AS Decimal(9, 5)), 1, 5, 10, 1)

    INSERT [dbo].[TestExport] ([RowNum], [SplitterName], [NumberOfRows], [NumberOfElements], [MinElementLength], [MaxElementLength], [Duration], [MinLength], [AvgLength], [MaxLength], [Run]) VALUES (2, N'DelimitedSplit8K', 1000, 1, 1, 10, CAST(0.01000 AS Decimal(9, 5)), 1, 5, 10, 1)

    INSERT [dbo].[TestExport] ([RowNum], [SplitterName], [NumberOfRows], [NumberOfElements], [MinElementLength], [MaxElementLength], [Duration], [MinLength], [AvgLength], [MaxLength], [Run]) VALUES (3, N'tvfDelimitedSplitXML1', 1000, 1, 1, 10, CAST(0.44000 AS Decimal(9, 5)), 1, 5, 10, 1)

    INSERT [dbo].[TestExport] ([RowNum], [SplitterName], [NumberOfRows], [NumberOfElements], [MinElementLength], [MaxElementLength], [Duration], [MinLength], [AvgLength], [MaxLength], [Run]) VALUES (4, N'Split', 1000, 2, 1, 10, CAST(0.03600 AS Decimal(9, 5)), 3, 11, 21, 2)

    INSERT [dbo].[TestExport] ([RowNum], [SplitterName], [NumberOfRows], [NumberOfElements], [MinElementLength], [MaxElementLength], [Duration], [MinLength], [AvgLength], [MaxLength], [Run]) VALUES (5, N'DelimitedSplit8K', 1000, 2, 1, 10, CAST(0.40000 AS Decimal(9, 5)), 3, 11, 21, 2)

    INSERT [dbo].[TestExport] ([RowNum], [SplitterName], [NumberOfRows], [NumberOfElements], [MinElementLength], [MaxElementLength], [Duration], [MinLength], [AvgLength], [MaxLength], [Run]) VALUES (6, N'tvfDelimitedSplitXML1', 1000, 2, 1, 10, CAST(0.41000 AS Decimal(9, 5)), 3, 11, 21, 2)

    INSERT [dbo].[TestExport] ([RowNum], [SplitterName], [NumberOfRows], [NumberOfElements], [MinElementLength], [MaxElementLength], [Duration], [MinLength], [AvgLength], [MaxLength], [Run]) VALUES (7, N'Split', 1000, 4, 1, 10, CAST(0.07300 AS Decimal(9, 5)), 9, 24, 41, 3)

    INSERT [dbo].[TestExport] ([RowNum], [SplitterName], [NumberOfRows], [NumberOfElements], [MinElementLength], [MaxElementLength], [Duration], [MinLength], [AvgLength], [MaxLength], [Run]) VALUES (8, N'DelimitedSplit8K', 1000, 4, 1, 10, CAST(0.03600 AS Decimal(9, 5)), 9, 24, 41, 3)

    INSERT [dbo].[TestExport] ([RowNum], [SplitterName], [NumberOfRows], [NumberOfElements], [MinElementLength], [MaxElementLength], [Duration], [MinLength], [AvgLength], [MaxLength], [Run]) VALUES (9, N'tvfDelimitedSplitXML1', 1000, 4, 1, 10, CAST(0.47300 AS Decimal(9, 5)), 9, 24, 41, 3)

    INSERT [dbo].[TestExport] ([RowNum], [SplitterName], [NumberOfRows], [NumberOfElements], [MinElementLength], [MaxElementLength], [Duration], [MinLength], [AvgLength], [MaxLength], [Run]) VALUES (10, N'Split', 1000, 8, 1, 10, CAST(0.05000 AS Decimal(9, 5)), 25, 51, 74, 4)

    INSERT [dbo].[TestExport] ([RowNum], [SplitterName], [NumberOfRows], [NumberOfElements], [MinElementLength], [MaxElementLength], [Duration], [MinLength], [AvgLength], [MaxLength], [Run]) VALUES (11, N'DelimitedSplit8K', 1000, 8, 1, 10, CAST(0.06000 AS Decimal(9, 5)), 25, 51, 74, 4)

    INSERT [dbo].[TestExport] ([RowNum], [SplitterName], [NumberOfRows], [NumberOfElements], [MinElementLength], [MaxElementLength], [Duration], [MinLength], [AvgLength], [MaxLength], [Run]) VALUES (12, N'tvfDelimitedSplitXML1', 1000, 8, 1, 10, CAST(0.68600 AS Decimal(9, 5)), 25, 51, 74, 4)

    INSERT [dbo].[TestExport] ([RowNum], [SplitterName], [NumberOfRows], [NumberOfElements], [MinElementLength], [MaxElementLength], [Duration], [MinLength], [AvgLength], [MaxLength], [Run]) VALUES (13, N'Split', 1000, 16, 1, 10, CAST(0.08300 AS Decimal(9, 5)), 65, 103, 140, 5)

    INSERT [dbo].[TestExport] ([RowNum], [SplitterName], [NumberOfRows], [NumberOfElements], [MinElementLength], [MaxElementLength], [Duration], [MinLength], [AvgLength], [MaxLength], [Run]) VALUES (14, N'DelimitedSplit8K', 1000, 16, 1, 10, CAST(0.12000 AS Decimal(9, 5)), 65, 103, 140, 5)

    INSERT [dbo].[TestExport] ([RowNum], [SplitterName], [NumberOfRows], [NumberOfElements], [MinElementLength], [MaxElementLength], [Duration], [MinLength], [AvgLength], [MaxLength], [Run]) VALUES (15, N'tvfDelimitedSplitXML1', 1000, 16, 1, 10, CAST(1.09000 AS Decimal(9, 5)), 65, 103, 140, 5)

    INSERT [dbo].[TestExport] ([RowNum], [SplitterName], [NumberOfRows], [NumberOfElements], [MinElementLength], [MaxElementLength], [Duration], [MinLength], [AvgLength], [MaxLength], [Run]) VALUES (16, N'Split', 1000, 32, 1, 10, CAST(0.10300 AS Decimal(9, 5)), 161, 206, 260, 6)

    INSERT [dbo].[TestExport] ([RowNum], [SplitterName], [NumberOfRows], [NumberOfElements], [MinElementLength], [MaxElementLength], [Duration], [MinLength], [AvgLength], [MaxLength], [Run]) VALUES (17, N'DelimitedSplit8K', 1000, 32, 1, 10, CAST(0.24300 AS Decimal(9, 5)), 161, 206, 260, 6)

    INSERT [dbo].[TestExport] ([RowNum], [SplitterName], [NumberOfRows], [NumberOfElements], [MinElementLength], [MaxElementLength], [Duration], [MinLength], [AvgLength], [MaxLength], [Run]) VALUES (18, N'tvfDelimitedSplitXML1', 1000, 32, 1, 10, CAST(1.86000 AS Decimal(9, 5)), 161, 206, 260, 6)

    INSERT [dbo].[TestExport] ([RowNum], [SplitterName], [NumberOfRows], [NumberOfElements], [MinElementLength], [MaxElementLength], [Duration], [MinLength], [AvgLength], [MaxLength], [Run]) VALUES (19, N'Split', 1000, 64, 1, 10, CAST(0.19000 AS Decimal(9, 5)), 348, 414, 483, 7)

    INSERT [dbo].[TestExport] ([RowNum], [SplitterName], [NumberOfRows], [NumberOfElements], [MinElementLength], [MaxElementLength], [Duration], [MinLength], [AvgLength], [MaxLength], [Run]) VALUES (20, N'DelimitedSplit8K', 1000, 64, 1, 10, CAST(0.43300 AS Decimal(9, 5)), 348, 414, 483, 7)

    INSERT [dbo].[TestExport] ([RowNum], [SplitterName], [NumberOfRows], [NumberOfElements], [MinElementLength], [MaxElementLength], [Duration], [MinLength], [AvgLength], [MaxLength], [Run]) VALUES (21, N'tvfDelimitedSplitXML1', 1000, 64, 1, 10, CAST(3.11000 AS Decimal(9, 5)), 348, 414, 483, 7)

    INSERT [dbo].[TestExport] ([RowNum], [SplitterName], [NumberOfRows], [NumberOfElements], [MinElementLength], [MaxElementLength], [Duration], [MinLength], [AvgLength], [MaxLength], [Run]) VALUES (22, N'Split', 1000, 128, 1, 10, CAST(0.34000 AS Decimal(9, 5)), 730, 831, 932, 8)

    INSERT [dbo].[TestExport] ([RowNum], [SplitterName], [NumberOfRows], [NumberOfElements], [MinElementLength], [MaxElementLength], [Duration], [MinLength], [AvgLength], [MaxLength], [Run]) VALUES (23, N'DelimitedSplit8K', 1000, 128, 1, 10, CAST(0.85300 AS Decimal(9, 5)), 730, 831, 932, 8)

    INSERT [dbo].[TestExport] ([RowNum], [SplitterName], [NumberOfRows], [NumberOfElements], [MinElementLength], [MaxElementLength], [Duration], [MinLength], [AvgLength], [MaxLength], [Run]) VALUES (24, N'tvfDelimitedSplitXML1', 1000, 128, 1, 10, CAST(6.30000 AS Decimal(9, 5)), 730, 831, 932, 8)

    INSERT [dbo].[TestExport] ([RowNum], [SplitterName], [NumberOfRows], [NumberOfElements], [MinElementLength], [MaxElementLength], [Duration], [MinLength], [AvgLength], [MaxLength], [Run]) VALUES (25, N'Split', 1000, 256, 1, 10, CAST(0.64600 AS Decimal(9, 5)), 1471, 1662, 1803, 9)

    INSERT [dbo].[TestExport] ([RowNum], [SplitterName], [NumberOfRows], [NumberOfElements], [MinElementLength], [MaxElementLength], [Duration], [MinLength], [AvgLength], [MaxLength], [Run]) VALUES (26, N'DelimitedSplit8K', 1000, 256, 1, 10, CAST(1.67600 AS Decimal(9, 5)), 1471, 1662, 1803, 9)

    SET IDENTITY_INSERT [dbo].[TestExport] OFF

    A procedure that will execute the dynamic SQL with the embedded BCP command

    --you only need to run these commands one time

    USE LocalTestDB --use your db of course

    GO

    EXEC sp_configure 'show advanced options', 1

    GO

    RECONFIGURE

    GO

    EXEC sp_configure 'xp_cmdshell', 1

    GO

    RECONFIGURE

    GO

    CREATE PROCEDURE [dbo].[sp_execresultset]

    @SQLToGetRecordSet nvarchar(max)

    ,@DatabaseToExecuteCodeIn sysname

    ,@debug bit = 0

    AS

    BEGIN

    DECLARE @sql nvarchar(max)

    DECLARE @AlteredSQL nvarchar(max)

    SET @sql='USE ' + @DatabaseToExecuteCodeIn + '

    DECLARE @DynamicSQL nvarchar(max)

    DECLARE @tbl TABLE(SQL nvarchar(max))

    SET @DynamicSQL = ''''

    INSERT INTO @tbl(SQL)

    ' + @SQLToGetRecordSet + '

    SELECT @DynamicSQL = @DynamicSQL + SQL FROM @tbl

    EXEC sp_ExecuteSQL @DynamicSQL'

    IF @debug=0

    EXEC sp_ExecuteSQL @sql

    ELSE

    PRINT @sql

    END

    GO

    Now you can define the query you want to export and run the stored procedure

    DECLARE

    @strSELECT NVARCHAR(4000)

    ,@cmd NVARCHAR(MAX)

    ,@minRow INT

    ,@maxRow INT

    ,@currDate DATETIME

    ,@strDate NVARCHAR(30)

    ,@strSQL NVARCHAR(MAX)

    -- this is only going to be used for the filename and can be changed as desired

    SET @currDate = GETDATE()

    SET @strDate = REPLACE(REPLACE(CONVERT(NVARCHAR(30),@currDate, 126),':',''),'.','')

    -- Your query

    SET @strSELECT = 'SELECT * FROM dbo.TestExport'

    -- To limit the query to specific rows

    SELECT

    @minRow = MIN(RowNum)

    ,@maxRow = MAX(RowNum)

    FROM

    dbo.TestExport

    WHERE

    RowNum BETWEEN 10 AND 20

    SET @cmd = ''

    -- I set the CSV filename to the date, but you can put whatever you want

    -- Use this to make use of min/max rows

    SET @cmd = N'SELECT ''EXEC master..xp_cmdshell ''''BCP "'+@strSELECT+'" QUERYOUT "C:\'+@strDate+'.csv" -w -t"," -F'+CAST(@minRow AS NVARCHAR(5))+' -L'+CAST(@maxRow AS NVARCHAR(5))+' -T -S"''+@@servername+''"'''''''+CHAR(10)

    -- or this to display ALL rows

    --SET @cmd = N'SELECT ''EXEC master..xp_cmdshell ''''BCP "'+@strSELECT+'" QUERYOUT "C:\'+@strDate+'.csv" -w -t"," -T -S"''+@@servername+''"'''''''+CHAR(10)

    --for testing

    --PRINT @cmd

    -- make sure to change the db names

    EXEC LocalTestDB..sp_execresultset @cmd, N'LocalTestDB',0

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply