Can we use table Datatype in BCP.

  • Hi,

    I have declared @tbl as table(id int, txt varchar(10) in my Stored Procedure.

    I inserted one record in it.

    Now i wand to send that record into text file.

    I tried in this way but it is giving error.

    exec xp_cmdshell 'Bcp dbname..@tbl out "D:\text.txt" --c -Pservername -Uusername -Ppassword..

    but iam getting error as 'Error at @tbl" .please give me any suggestion.

    By

    Ram

  • Hi Ramesh,

    Table data type can be used only in the session it is declared. You can not access it directly outside the context of the declared session. The best to use is write a procedure and then execute the same in the BCP. Use the sample code to build your own.

    ===========================================

    CREATE PROCEDURE dbo.testProc

    AS

    BEGIN

    SET NOCOUNT ON

    DECLARE @tbl TABLE(id VARCHAR(20), txt varchar(40))

    BEGIN

    INSERT INTO @tbl SELECT au_id, au_lname FROM pubs.dbo.authors

    SELECT * FROM @tbl

    END

    END

    ===========================================

    /* To Ouput to a file use this bcp command

    EXEC master..xp_cmdshell'BCP "exec mydb..testproc" queryout C:\OutputfromTable.txt -c -S myServer -T'

    ============================================

    Good Luck

    -Sravan

  • hai sravan,

    exec xp_cmdshell 'Bcp dbname..@tbl out "D:\text.txt" --c -Pservername -Uusername -Ppassword..This line is in the stored procedure only. Still it is giving error.

    Please help me

    Ramesh

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

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