Issue with OSQL

  • Hi Friends,

    Requirement : i have a table which contains around 40 columns of which name is a column which i use to segregate the data because the contains the data for multiple names. Extract data for a NAME and store that in a file in windows directory.

    what i did:

    i have written a stored proc to which i send NAME as a parameter.The proc extracts all the data for that NAME. So, i have used OSQL utility to generate the output in a file( should be generated in a windows directory). I have around 30 to 40 NAMES.So, i have a cursor which loops through the NAMES and calls OSQL for each and every NAME.OSQL command is assigned to a string variable and that string is passed as a parameter to xp_cmdshell.

    Issue: whatever i wrote after OSQL command(execute xp_cmdshell), not getting executed.Also, if i pass 40 NAMES , only 20 to 30 files are getting created and rest of the files are not getting generated at all.

    Please advise your valuable suggestion.

  • adding to the below, i am using sql server 2005.

  • A few things:

    1) osql is deprecated in SQL 2005. Consider switching to use sqlcmd or bcp for this work.

    2) accessing the file system from T-SQL can create some scenarios that are difficult to debug. Consider switching from running this process in a cursor in T-SQL to something in SSIS or PowerShell so you can move away from using xp_cmdshell. Moving in this direction could make item 1 moot since SSIS and PowerShell both have the ability to write to files natively, i.e. without the help of SQL Server command lines tools.

    3) We cannot see what you see. Please post the code that includes the cursor and the call to xp_cmdshell so we can see too. Make sure to cleanse any sensitive information from the code before posting including items in code comments and object names that may reveal company or personal information.

    There are no special teachers of virtue, because virtue is taught by the whole community.
    --Plato

  • sorry for the delay as i am not in town.

    below is the code snippet. on an average this cursor has to generate around 90 files and this cursor runs every 15min.

    ----------------------------------------

    declare my_cursor CURSOR for

    select distinct WRS.f1 , WRS.c1 from tab2 WCB

    inner join tab1 WRS on WCB.c1=WRS.c1

    OPEN my_cursor

    FETCH NEXT FROM my_cursor

    INTO @f1, @c1

    WHILE @@FETCH_STATUS = 0

    BEGIN

    set @nvchProcedure = 'proc name'

    select @ts = convert(varchar, getdate(),112)+replace(convert(varchar, getdate(),108),':','') -- suffix the file name with datetimestamp

    select @ff1 = @f1 + @ts + '.psv'

    insert into tab3 (c1,f1,ff1)values (@c1,@f1,@ff1)

    SELECT @cmd = 'osql -S ' + @@servername +' -E -n -h-1 -w2500 -q "SET NOCOUNT ON EXEC ' + @HostDB + '.dbo.' + @nvchProcedure +' '''+@c1 +''''+',''''+'" -o "'+ @nvchFileLocation +@f1+@ts+'.psv'

    EXEC master..xp_cmdshell @cmd

    FETCH NEXT FROM my_cursor

    INTO @f1, @c1

    END

    CLOSE my_cursor

    DEALLOCATE my_cursor

    ----------------------------------------

  • What comes to mind initially is that @cmd is sometimes ending up NULL due to @c1 or @t1 (coming from the cursor-select) being NULL since anything+NULL=NULL. Try adding some logging or some RAISERROR commands with severity=10 to see what @cmd is set to when it stops generating files.

    There are no special teachers of virtue, because virtue is taught by the whole community.
    --Plato

  • there would be values in C1 and T1 for sure.They never be null

  • Does @f1 ever have a character in it that would be considered invalid in a Windows file name?

    Do you know what I mean when I say add calls to "RAISERROR with severity 10"?

    There are no special teachers of virtue, because virtue is taught by the whole community.
    --Plato

  • No, @f1 has only underscore as the special character reamining all are letters/digits.

    Could you please explain more on RAISEERROR with sev 10?

  • Compassionate (3/5/2013)


    No, @f1 has only underscore as the special character reamining all are letters/digits.

    Could you please explain more on RAISEERROR with sev 10?

    You can add calls to RAISERROR with a severity of 10 or less which will send an informational message to the output stream.

    Here is a generic call that sends a literal string to the output stream:

    RAISERROR('Custom informational message.', 10, 1);

    Another way to call RAISERROR that might be useful in your case is to use tokens to send variable values to the output stream, like this:

    RAISERROR('@f1 = %s', 10, 1, @f1);

    What's happening in the above statement is that the value of @f1 is being substituted into the place of the token %s, which means "a string value", within the message before it is sent to the output stream There is a different token for numbers, %d, and some other tokens for different data types as well. Unfortunately there is no token for date or time values.

    You can read more here:

    RAISERROR (Transact-SQL) - SQL Server 2005

    There are no special teachers of virtue, because virtue is taught by the whole community.
    --Plato

Viewing 9 posts - 1 through 8 (of 8 total)

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