help with SQLCMD

  • Hi ,

    I am trying to use SQLCMD to run a SQL Script. The script is of type.

    Begin tran

    Delete .....

    Insert .....

    Commit tran

    The output captured doesnt show the proper output. Only rows affected by the DELETE statement is captured. Rows affected by the insert statement is not captured.

    My SQLCMD statement looks like....

    master.dbo.xp_cmdshell 'sqlcmd -SABC -d DEF -u -i "G:\fct_t.sql" -o "G:\fct_t.txt"'

    Please help!!!

    Cheers

  • AppSup_dba (8/24/2012)


    Hi ,

    I am trying to use SQLCMD to run a SQL Script. The script is of type.

    Begin tran

    Delete .....

    Insert .....

    Commit tran

    The output captured doesnt show the proper output. Only rows affected by the DELETE statement is captured. Rows affected by the insert statement is not captured.

    My SQLCMD statement looks like....

    master.dbo.xp_cmdshell 'sqlcmd -SABC -d DEF -u -i "G:\fct_t.sql" -o "G:\fct_t.txt"'

    Please help!!!

    This is not very clear to me.

    What output are you referring to? You would not typically expect any if you action an Insert or Delete unless you are reading from the Inserted/Deleted Tables?

  • Thanks for looking into this... I will try to make it make clear....

    Actual output:

    Starting the Transaction.

    Beginning to run the queries.

    (0 rows affected)

    Queries executed successfully.

    Transaction committed successfully.

    Expected output:

    Starting the Transaction.

    Beginning to run the queries.

    (0 rows affected)

    (1 rows affected)

    (1 rows affected)

    (1 rows affected)

    Queries executed successfully.

    Transaction committed successfully.

    Cheers

  • Unfortunately, we can't see what you see. You haven't given us enough information to be able to help you. Based on what has been posted, I can't even give you a shot in the dark.

  • AppSup_dba (8/24/2012)


    Thanks for looking into this... I will try to make it make clear....

    Actual output:

    Starting the Transaction.

    Beginning to run the queries.

    (0 rows affected)

    Queries executed successfully.

    Transaction committed successfully.

    Expected output:

    Starting the Transaction.

    Beginning to run the queries.

    (0 rows affected)

    (1 rows affected)

    (1 rows affected)

    (1 rows affected)

    Queries executed successfully.

    Transaction committed successfully.

    Is there any conditional logic in there?

    How did you determine that was the expected output?

    You ran the query with another tool?

  • Lynn, OTF,

    I think what OP is talking about is redirecting STDOUT to a file (specified by the -o switch). This supposed to capture all output produced by SQL statements in the input file (specified by -i) and write them to a file.

    Ankur,

    Are you sure there are no errors? Try to setup error log as well.

    For Reference:

    http://msdn.microsoft.com/en-us/library/ms162773(v=sql.105).aspx

    --Vadim R.

  • Even I tried this procedure. The link which you provided helped a lot. Thanks for providing the link.

  • In hurry to get my issue resolved i think i didnt provide enough information. My bad. I will try to make thing more clear now and provide scripts so that you can recreate the scenario.

    My table structure:

    CREATE TABLE [dbo].[test_t](

    [ser] [nvarchar](100) NULL,

    [exp_date] [date] NULL

    ) ON [PRIMARY]

    SQL query that i intend to run:

    DELETE FROM dbo.test_t WHERE ser = 'cd5546';

    INSERT INTO dbo.test_t VALUES ('ab3546','2012-04-19')

    INSERT INTO dbo.test_t VALUES ('ab4546','2012-04-19')

    INSERT INTO dbo.test_t VALUES ('cd5546','2012-04-19')

    When in run this query from MS, i get the following output(As expected):

    (0 row(s) affected)

    (1 row(s) affected)

    (1 row(s) affected)

    (1 row(s) affected)

    When i run this query using SQLCMD in below syntax, after saving the sql query as C:\SQL\SQLQuery2.sql

    master.dbo.xp_cmdshell 'sqlcmd -Sserver -d database -W -i "C:\SQL\SQLQuery2.sql" -o "C:\SQL\Output\SQLQuery2.sql"'

    The output file that gets created as C:\SQL\Output\SQLQuery2.sql contains only below text.

    (0 rows affected)

    The number of rows affected by the Insert statements doesnt get captured. Actually only the output of first SQL statement is captured.

    Though i tested that if i run my statements in batches, i get desired results. But i dont understand the behavior in above scripts.

    I can provide any more info as required. Thanks people for your help... I great appreciate your support.

    Cheers

  • I will be happy to provide any more information in this regard....

    Cheers

  • Did you check if the records are actually inserted by SQLCMD run?

    _____________
    Code for TallyGenerator

  • Yes records were inserted... i verified that...

    Cheers

  • Okay, I've just run a quick check and all the statements are actioned, however, only the first "rows affected" message seems to be returned.

    This behaviour had been reported as a bug a while back.

    https://connect.microsoft.com/sql/Feedback/ViewFeedback.aspx?FeedbackID=309251

    I've run a quick check against a Sql 2012 Instance and it appears to now work as expected.

    I guess its been fixed in that version, as promised.

  • Thanks for the info OTF... i will try this with OSQL...

    Cheers

  • AppSup_dba (8/30/2012)


    Thanks for the info OTF... i will try this with OSQL...

    No worries.

    As I understand it, OSQL works fine in that regard in Sql Server 2008.

  • I have this problem too.

    I upgraded to SSMS 2008 R2 and now have sqlcmd.exe version 2009.100.1600.1

    Previous version was SSMS 2005

    Used to be that when using the -o option I would get all messages output to a file.

    This includes

    errors,

    print statement output, and

    ### row(s) affected statements.

    Now, when I run scripts with multiple update and/or multiple insert statements I only see "### row(s) affected " for the first such statement. This is very disconcerting. I depend on reading the output file to see if the expected number of rows were affected by each statement.

    For select statements I do get the row(s) affected count, as before. Even when there are multiple insert and update statements interceding. I have not done any deletes in these scripts.

    Each script is a single batch. One poster mentioned that they get the row(s) affected for each insert statement IFF they put each insert statement into a separate batch. This is not useful to me as some declared variables are used through out the script.

    I tried adding SET NOCOUNT OFF before each insert and update statement to try to enforce that I want the counts to be reported. No luck.

    This is really aggravating.

Viewing 15 posts - 1 through 15 (of 16 total)

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