Running a dynamic query using sql

DECLARE @script VARCHAR(MAX);
SET @script = 
    '
    create table ali(id decimal(10,0));
    drop table ali;
    go
    create table ali(id decimal(10,0));
    drop table ali;
    '

EXEC (@script);

An error message occurred while executing the request above. Please tell me if you have an idea to solve this problem.

Msg 102, Level 15, State 1, Line 4 Invalid syntax next to "go".

Note: the above code for creating and deleting the created table is, for example, I have other dynamic queries with go statement. Please do not give an answer.

DECLARE @script   VARCHAR(MAX),
        @script1  VARCHAR(MAX);
SET @script = 
    '
    create table ali(id decimal(10,0));
    drop table ali;
    ';
SET @script1 = 
    '
    create table ali(id decimal(10,0));
    drop table ali;
    ';
EXEC (@script);
EXEC (@script1);
+13
source share
5 answers

GOactually invalid T-SQL :

GO is not a Transact-SQL statement; it is a team recognized by sqlcmd and the osql utility and SQL Server Management Studio code editor.

GO SQL , (, osql ). - GO, SQL.

+14

.

, "GO".

:

--Your Script modified by adding a single line of code:
DECLARE @script nVarChar(MAX);--I changed from VarChar to nVarChar - you should always use nVarChar for Dynamic SQL.
SET @script = 
    '
    create table ali(id decimal(10,0));
    drop table ali;
    go
    create table ali(id decimal(10,0));
    drop table ali;
    '
    --In case you have apostrophes in your script, you must escape them for the Exec() command. - 03/14/2013 - MCR.
SET @script = 'EXEC (''' + REPLACE(REPLACE(@script, '''', ''''''), 'GO', '''); EXEC(''') + ''');'--Just add this one line.
PRINT @script --See the command used (will be truncated in Select/Print, but not when Executing).
EXEC (@script);


, , :

--Example of compiling and chaining multiple DDL statments from data in a table:
-- DDL (Data Definition Language).
--  These are statements used to create, alter, or drop data structures.
--  They MUST be run in a single Batch.
--  The "GO" keyword is a SSMS syntax for splitting up batches - it is not an SQL keyword.
DECLARE @DDL_Statements TABLE
(
  DDL nVarChar(MAX)
)
INSERT INTO @DDL_Statements (DDL)
    SELECT 'create table ali(id decimal(10,0)); drop table ali;' UNION ALL
    SELECT 'create table ali(id decimal(10,0)); drop table ali;'
DECLARE @SqlCommand nVarChar(MAX) = ''
 SELECT @SqlCommand = @SqlCommand + 'EXEC(''' + REPLACE(DS.DDL, '''', '''''') + '''); '
   FROM @DDL_Statements as DS --In case you have apostrophes in your script, you must escape them for the Exec() command. - 03/14/2013 - MCR.
PRINT @SqlCommand --See the command used (will be truncated in Select/Print, but not when Executing).
EXEC (@SqlCommand)
+6

GO T-SQL-, @mellamokb.

SQL-, , , .

GO UDF. .

, GO, .

+3

GO - , , , SQL. , SSMS - Tools - Options - Query Execution - SQL Server , COME, .

, GO SSMS. SQL . GO

+3

, GO SQL, GO n n, , .

        DECLARE @statement TABLE
        (
          SqlCommand NVARCHAR(MAX)
        )

DECLARE @sqlCommand NVARCHAR(MAX) = 'INSERT INTO [dbo].[tbl_table1] ([Field1],[Field2],[Field3],[Field4],[Field5],[Field6],[Field7],[Field8],[Field9],[Field10],[Field11])SELECT ''a'',''b'',''c'',''d'',''e'',''f'',''g'',''h'',''i'',''j'',RAND(),RAND(),RAND()
GO 10000'
INSERT  INTO @statement
            ( SqlCommand )
    VALUES  ( @sqlCommand )
    SET @sqlCommand = REPLACE(@sqlCommand, '[tbl_table1]', '[tbl_table2]')
    INSERT  INTO @statement
            ( SqlCommand )
    VALUES  ( @sqlCommand )

, select, ,

EXEC(@sqlCommand)

Which gives the wrong syntax around the error "10000".

+1
source

Source: https://habr.com/ru/post/1669699/


All Articles