• srajinigandh (9/18/2013)


    ramakantshankar (9/13/2011)


    Hi,

    As you said we can use the SQLCMD to execute operating system commands by indicating the " !! " before the command.

    I have created a POC on using OPerating System Commands in a Stored procedure.

    Following is the Code for the created Stored Procedure. In the Script for Procedure the Operating Systems commands are used:

    CREATE PROCEDURE SQLCMD_TEST

    AS

    !!MKDIR "C:\TEST"

    :OUT "C:\TEST\test.TXT"

    SELECT @@VERSION AS 'SERVER VERSION'

    !!DIR

    GO

    SELECT @@SERVERNAME AS 'SERVER NAME'

    GO

    I am able to successfully execute the procedure:EXEC SQLCMD_TEST

    Also if required you can keep the Execute procedure in a SQL script and pass the Script location to the SQLCMD syntax as INPUT file.

    Please revert back if this works for you.

    Hi ramakantshankar ,

    I understand this script was working for you and its some time back , just wondering which version did you try this and its notworking in Sql server 2012.Any ideas please ?

    Error is 'Incorrect syntax near !'

    Regards

    Raj

    Assuming you are running the code in Sql Server Management Studio, the error you are getting is probably because the editor is not in Sql Cmd Mode.

    In order to do this, you will have to select "Sql Cmd Mode" from the "Query" Menu Items.

    Now that being said, encapsulating those statements within a Stored Procedure is a new one on me.

    I'm not so sure that is valid.

    How will that Procedure run without directly invoking SqlCmd?