diff --git a/contrib/babelfishpg_tsql/antlr/TSqlParser.g4 b/contrib/babelfishpg_tsql/antlr/TSqlParser.g4 index 50a974d5d5..99fcf7bb80 100644 --- a/contrib/babelfishpg_tsql/antlr/TSqlParser.g4 +++ b/contrib/babelfishpg_tsql/antlr/TSqlParser.g4 @@ -358,7 +358,7 @@ raiseerror_statement ; raiseerror_msg - : DECIMAL | char_string | LOCAL_ID + : DECIMAL | char_string | local_id ; raiseerror_option @@ -2004,7 +2004,7 @@ func_body_return_select_body ; func_body_returns_table - : RETURNS LOCAL_ID table_type_definition + : RETURNS local_id table_type_definition (WITH function_option (COMMA function_option)*)? AS? BEGIN sql_clauses* SEMI? END @@ -2031,7 +2031,7 @@ atomic_func_body // CREATE PROC p @p INT NULL --> this appears to be accepted syntax for non-native compiled procs, though formally not allowed procedure_param - : LOCAL_ID AS? data_type VARYING? (NOT? NULL_P)? (EQUAL default_val=expression)? param_option=(OUT | OUTPUT | READONLY)? + : local_id AS? data_type VARYING? (NOT? NULL_P)? (EQUAL default_val=expression)? param_option=(OUT | OUTPUT | READONLY)? ; // drop_procedure_param can be used in a DROP FUNCTION or DROP PROCEDURE command @@ -2640,7 +2640,7 @@ predict_function // https://msdn.microsoft.com/en-us/library/ms188927.aspx declare_statement - : DECLARE LOCAL_ID AS? table_type_definition SEMI? + : DECLARE local_id AS? table_type_definition SEMI? | DECLARE loc+=declare_local (COMMA loc+=declare_local)* SEMI? ; @@ -2842,7 +2842,7 @@ execute_statement_arg ; execute_statement_arg_named - : name=LOCAL_ID EQUAL value=execute_parameter + : name=local_id EQUAL value=execute_parameter ; execute_statement_arg_unnamed @@ -2851,13 +2851,13 @@ execute_statement_arg_unnamed execute_parameter : constant - | LOCAL_ID (OUTPUT | OUT)? + | local_id (OUTPUT | OUT)? | id | DEFAULT ; execute_var_string - : LOCAL_ID + : local_id | char_string ; @@ -3098,21 +3098,21 @@ set_statement // https://msdn.microsoft.com/en-us/library/ms174377.aspx transaction_statement // https://msdn.microsoft.com/en-us/library/ms188386.aspx - : BEGIN DISTRIBUTED (TRAN | TRANSACTION) (id | LOCAL_ID)? SEMI? + : BEGIN DISTRIBUTED (TRAN | TRANSACTION) (id | local_id)? SEMI? // https://msdn.microsoft.com/en-us/library/ms188929.aspx - | BEGIN (TRAN | TRANSACTION) ((id | LOCAL_ID) (WITH MARK char_string)?)? SEMI? + | BEGIN (TRAN | TRANSACTION) ((id | local_id) (WITH MARK char_string)?)? SEMI? // https://msdn.microsoft.com/en-us/library/ms190295.aspx - | COMMIT (TRAN | TRANSACTION) (id | LOCAL_ID)? (WITH LR_BRACKET DELAYED_DURABILITY EQUAL (OFF | ON) RR_BRACKET )? SEMI? + | COMMIT (TRAN | TRANSACTION) (id | local_id)? (WITH LR_BRACKET DELAYED_DURABILITY EQUAL (OFF | ON) RR_BRACKET )? SEMI? // https://msdn.microsoft.com/en-us/library/ms178628.aspx | COMMIT WORK? SEMI? | COMMIT id | ROLLBACK id // https://msdn.microsoft.com/en-us/library/ms181299.aspx - | ROLLBACK (TRAN | TRANSACTION) (id | LOCAL_ID)? SEMI? + | ROLLBACK (TRAN | TRANSACTION) (id | local_id)? SEMI? // https://msdn.microsoft.com/en-us/library/ms174973.aspx | ROLLBACK WORK? SEMI? // https://msdn.microsoft.com/en-us/library/ms188378.aspx - | SAVE (TRAN | TRANSACTION) (id | LOCAL_ID)? SEMI? + | SAVE (TRAN | TRANSACTION) (id | local_id)? SEMI? ; // https://msdn.microsoft.com/en-us/library/ms188366.aspx @@ -3135,7 +3135,7 @@ shutdown_statement ; dbcc_statement - : DBCC CHECKIDENT ( LR_BRACKET table_name_string ( (COMMA NORESEED) | (COMMA RESEED (COMMA MINUS? new_value=(DECIMAL | FLOAT))?) )? RR_BRACKET ) (WITH dbcc_options)? SEMI? + : DBCC CHECKIDENT ( LR_BRACKET dbcc_table_name ( (COMMA NORESEED) | (COMMA RESEED (COMMA MINUS? checkident_new_value)?) )? RR_BRACKET ) (WITH dbcc_options)? SEMI? | DBCC name=dbcc_command ( LR_BRACKET expression_list RR_BRACKET )? (WITH dbcc_options)? SEMI? //These are dbcc commands with strange syntax that doesn't fit the regular dbcc syntax | DBCC SHRINKLOG ( LR_BRACKET SIZE EQUAL (constant_expression| id | DEFAULT) (KB | MB | GB | TB)? RR_BRACKET )? (WITH dbcc_options)? SEMI? @@ -3175,12 +3175,17 @@ dbcc_options : ID (COMMA ID)? ; - -table_name_string +dbcc_table_name : table = id | char_string + | local_id ; +checkident_new_value + : DECIMAL + | FLOAT + | local_id + ; execute_as_clause : (EXECUTE|EXEC) AS (CALLER | SELF | OWNER | char_string) @@ -3459,7 +3464,7 @@ set_offsets_keyword constant_LOCAL_ID : constant - | LOCAL_ID + | local_id ; // https://docs.microsoft.com/en-us/sql/t-sql/language-elements/expressions-transact-sql diff --git a/contrib/babelfishpg_tsql/src/pl_comp.c b/contrib/babelfishpg_tsql/src/pl_comp.c index db0d3e0282..7ccc2380fe 100644 --- a/contrib/babelfishpg_tsql/src/pl_comp.c +++ b/contrib/babelfishpg_tsql/src/pl_comp.c @@ -535,8 +535,16 @@ do_compile(FunctionCallInfo fcinfo, */ if (function->is_mstvf) { + /* + * For a user-defined @@var or @var# name, + * delimit with square brackets + */ + char *typname_fmt = "%s.\"%s\""; + if (!is_tsql_atatuservar(argdtype->typname)) + typname_fmt = pstrdup("%s.%s"); + tbl_dno = argvariable->dno; - tbl_typ = psprintf("%s.%s", + tbl_typ = psprintf(typname_fmt, get_namespace_name( get_rel_namespace(get_typ_typrelid(argtypeid))), argdtype->typname); diff --git a/contrib/babelfishpg_tsql/src/pl_exec-2.c b/contrib/babelfishpg_tsql/src/pl_exec-2.c index 7bc268f911..ec02108b0b 100644 --- a/contrib/babelfishpg_tsql/src/pl_exec-2.c +++ b/contrib/babelfishpg_tsql/src/pl_exec-2.c @@ -132,6 +132,9 @@ static bool prev_insert_bulk_check_constraints = false; /* return a underlying node if n is implicit casting and underlying node is a certain type of node */ static Node *get_underlying_node_from_implicit_casting(Node *n, NodeTag underlying_nodetype); + +/* Enclose a user-defined @@var or @var# name in delimiters */ +static char *delimit_tsql_atatuservar(const char *src); /* * The pltsql_proc_return_code global variable is used to record the @@ -1365,6 +1368,7 @@ static int exec_stmt_decl_table(PLtsql_execstate *estate, PLtsql_stmt_decl_table *stmt) { char *tblname; + char *tblname_create; char *query; PLtsql_tbl *var = (PLtsql_tbl *) (estate->datums[stmt->dno]); int rc; @@ -1389,8 +1393,8 @@ exec_stmt_decl_table(PLtsql_execstate *estate, PLtsql_stmt_decl_table *stmt) /* * If the original refname was already >=63 characters (the max limit of PG identifiers), - * then the above construction of tblname will be >63 characters, which will violate the - * max length of PG identiefiers and cause issues down the road. Fix this by truncating + * then the above construction of tblname will be >63 characters, which will exceed the + * max length of PG identifiers and cause issues down the road. Fix this by truncating * tblname so that adding the "_<@@nestlevel>" suffix will be exactly 63 characters. */ if (strlen(tblname) >= NAMEDATALEN) @@ -1401,12 +1405,20 @@ exec_stmt_decl_table(PLtsql_execstate *estate, PLtsql_stmt_decl_table *stmt) tblname = psprintf("%s_%d", tblname, estate->nestlevel); } + /* + * Add delimiters for valid T-SQL variable names like @@var or @var# + */ + if (is_tsql_atatuservar(tblname)) + tblname_create = psprintf("[%s]", tblname); + else + tblname_create = psprintf("%s", tblname); + if (stmt->tbltypname) query = psprintf("CREATE TEMPORARY TABLE IF NOT EXISTS %s (like %s including all)", - tblname, stmt->tbltypname); + tblname_create, stmt->tbltypname); else query = psprintf("CREATE TEMPORARY TABLE IF NOT EXISTS %s%s", - tblname, stmt->coldef); + tblname_create, stmt->coldef); /* * If a table with the same name already exists, we should just use @@ -1475,7 +1487,15 @@ exec_stmt_return_table(PLtsql_execstate *estate, PLtsql_stmt_return_query *stmt) oldcontext = MemoryContextSwitchTo(estate->func->fn_cxt); expr = palloc0(sizeof(PLtsql_expr)); - expr->query = psprintf("select * from %s", tbl->tblname); + + /* + * Add delimiters for valid T-SQL variable names like @@var or @var# + */ + if (is_tsql_atatuservar(tbl->tblname)) + expr->query = psprintf("select * from [%s]", tbl->tblname); + else + expr->query = psprintf("select * from %s", tbl->tblname); + expr->plan = NULL; expr->paramnos = NULL; expr->rwparam = -1; @@ -2494,6 +2514,179 @@ is_char_identpart(char c) (c >= '0' && c <= '9')); } +/* + * Check for allowed chars in @variable name + * ToDo: support non-standard ASCII chars (Unicode ranges) + * and align with is_identifier_char() + */ +static inline bool +is_variable_name_char(unsigned char c) +{ + bool valid = ( + isalpha(c) || + isdigit(c) || + c == '_' || + c == '@' || + c == '$' || + c == '#' + ); + + return valid; +} + +/* + * Put delimiters around a T-SQL variable/parameter that is + * named '@@var' or contains a hash, e.g. '@var#'. + * Without delimiters, the backend will raise an error. + * This is used for the parameter argument of sp_executesql, so the input + * string may contain multiple names, e.g.: @par1 int, @par2 varchar(20), ... + * This function calls palloc() to allocate a new string and returns a pointer + * to this string. + */ +static char * +delimit_tsql_atatuservar(const char *src) +{ + char *s = (char *) src; + char *varname_start = NULL; + bool add_delimiter = false; + + /* + * Reserving twice the amount of space of the input string: since the shortest possible + * parameter definition is 5 characters ('@@p x' , where x would be the type), this will + * always be enough for adding delimiters. + * Note that there can be multiple parameter names in the input string. + */ + char *result = (char *) palloc(sizeof(char)*strlen(src)*2); + char *tgt = result; + + while (*s) + { + /* Look for start of variable name, which is always '@' */ + if (*s != '@') + { + *tgt++ = *s++; + continue; + } + + /* Start of variable name found */ + add_delimiter = false; + + varname_start = s; + + /* Name starting with @@ */ + if (*(s+1)) + { + if (*(s+1) == '@') + { + add_delimiter = true; + } + } + + /* Find end of variable name */ + while (*s) + { + /* Check for allowed chars in @variable name */ + if (is_variable_name_char(*s)) + { + /* Name contains # */ + if (*s == '#') + { + add_delimiter = true; + } + + s++; + } + else + { + break; + } + } + + if (varname_start != src) + { + /* + * Do not add delimiters if the name is already delimited. + * Both square brackets and double quotes are used as delimiters for variable names. + */ + if ((*(varname_start-1) == '[') || (*(varname_start-1) == '"')) + { + add_delimiter = false; + } + } + + // Add delimiters to the name if required + if (add_delimiter) *tgt++ = '['; + while (varname_start != s) + { + *tgt++ = *varname_start++; + } + if (add_delimiter) *tgt++ = ']'; + } /* while */ + + *tgt = '\0'; + return result; +} + +/* + * Determine whether the passed name is a T-SQL variable/parameter name that is + * named '@@var' or contains a hash, e.g. '@var#'. + */ +bool +is_tsql_atatuservar(const char *varname) +{ + char *s = (char *) varname; + bool is_atatuservar = false; + + /* The variable names we're looking for are at least 3 chars */ + if (strlen(varname) <= 2) + { + return false; + } + + /* Variable name must start with '@' */ + if (*s != '@') + { + return false; + } + + /* Starts with '@@' ? */ + s++; + if (*s == '@') + { + is_atatuservar = true; + } + + while (*s) + { + /* Check for allowed chars in @variable name */ + if (is_variable_name_char(*s)) + { + /* Name contains # */ + if (*s == '#') + { + is_atatuservar = true; + } + + s++; + } + else + { + return false; + } + } /* while */ + + /* + * The variable name should continue until end of string; if not, + * something is wrong + * + * NB: The assertion below is logically true given the loop above, + * but kept in the code for clarity. + */ + Assert(*s == '\0'); + + return is_atatuservar; +} + /* * Read parameter definitions */ @@ -2519,10 +2712,11 @@ read_param_def(InlineCodeBlockArgs *args, const char *paramdefstr) /* * Create a fake CREATE PROCEDURE statement to get the param definition * parse tree. + * Delimiters will be applied around parameter names like @@par or @par#. */ initStringInfo(&proc_stmt); appendStringInfoString(&proc_stmt, str1); - appendStringInfoString(&proc_stmt, paramdefstr); + appendStringInfoString(&proc_stmt, delimit_tsql_atatuservar(paramdefstr)); appendStringInfoString(&proc_stmt, str2); parsetree = raw_parser(proc_stmt.data, RAW_PARSE_DEFAULT); diff --git a/contrib/babelfishpg_tsql/src/pl_exec.c b/contrib/babelfishpg_tsql/src/pl_exec.c index f0dae9ebd7..efada3514d 100644 --- a/contrib/babelfishpg_tsql/src/pl_exec.c +++ b/contrib/babelfishpg_tsql/src/pl_exec.c @@ -10110,7 +10110,6 @@ pltsql_clean_table_variables(PLtsql_execstate *estate, PLtsql_function *func) int rc; PLtsql_tbl *tbl; bool old_pltsql_explain_only = pltsql_explain_only; - const char *query_fmt = "DROP TABLE %s"; const char *query; bool old_abort_curr_txn = AbortCurTransaction; @@ -10132,7 +10131,13 @@ pltsql_clean_table_variables(PLtsql_execstate *estate, PLtsql_function *func) if (!tbl->need_drop) continue; - query = psprintf(query_fmt, tbl->tblname); + /* + * Use delimiters for names like @@var or @var# + */ + if (is_tsql_atatuservar(tbl->tblname)) + query = psprintf("DROP TABLE [%s]", tbl->tblname); + else + query = psprintf("DROP TABLE %s", tbl->tblname); pltsql_explain_only = false; /* Drop temporary table even in * EXPLAIN ONLY mode */ diff --git a/contrib/babelfishpg_tsql/src/pl_funcs-2.c b/contrib/babelfishpg_tsql/src/pl_funcs-2.c index 12774b168c..618db9ebb5 100644 --- a/contrib/babelfishpg_tsql/src/pl_funcs-2.c +++ b/contrib/babelfishpg_tsql/src/pl_funcs-2.c @@ -405,6 +405,57 @@ check_restricted_stored_procedure(Oid proc_id) } } +/* Determine whether a variable name is a predefined T-SQL global variable */ +bool +is_tsql_atatglobalvar(const char *varname) +{ + size_t varname_len = strlen(varname); + if ((varname_len < 6) || (varname_len > 18)) + return false; + + // List of all T-SQL global "@@" variables: + if ( + (pg_strcasecmp("@@CONNECTIONS", varname) == 0) || + (pg_strcasecmp("@@CPU_BUSY", varname) == 0) || + (pg_strcasecmp("@@CURSOR_ROWS", varname) == 0) || + (pg_strcasecmp("@@DATEFIRST", varname) == 0) || + (pg_strcasecmp("@@DBTS", varname) == 0) || + (pg_strcasecmp("@@ERROR", varname) == 0) || + (pg_strcasecmp("@@PGERROR", varname) == 0) || // added by Babelfish + (pg_strcasecmp("@@FETCH_STATUS", varname) == 0) || + (pg_strcasecmp("@@IDENTITY", varname) == 0) || + (pg_strcasecmp("@@IDLE", varname) == 0) || + (pg_strcasecmp("@@IO_BUSY", varname) == 0) || + (pg_strcasecmp("@@LANGID", varname) == 0) || + (pg_strcasecmp("@@LANGUAGE", varname) == 0) || + (pg_strcasecmp("@@LOCK_TIMEOUT", varname) == 0) || + (pg_strcasecmp("@@MAX_CONNECTIONS", varname) == 0) || + (pg_strcasecmp("@@MAX_PRECISION", varname) == 0) || + (pg_strcasecmp("@@NESTLEVEL", varname) == 0) || + (pg_strcasecmp("@@OPTIONS", varname) == 0) || + (pg_strcasecmp("@@PACKET_ERRORS", varname) == 0) || + (pg_strcasecmp("@@PACK_RECEIVED", varname) == 0) || + (pg_strcasecmp("@@PACK_SENT", varname) == 0) || + (pg_strcasecmp("@@PROCID", varname) == 0) || + (pg_strcasecmp("@@REMSERVER", varname) == 0) || + (pg_strcasecmp("@@ROWCOUNT", varname) == 0) || + (pg_strcasecmp("@@SERVERNAME", varname) == 0) || + (pg_strcasecmp("@@SERVICENAME", varname) == 0) || + (pg_strcasecmp("@@SPID", varname) == 0) || + (pg_strcasecmp("@@TEXTSIZE", varname) == 0) || + (pg_strcasecmp("@@TIMETICKS", varname) == 0) || + (pg_strcasecmp("@@TOTAL_ERRORS", varname) == 0) || + (pg_strcasecmp("@@TOTAL_READ", varname) == 0) || + (pg_strcasecmp("@@TOTAL_WRITE", varname) == 0) || + (pg_strcasecmp("@@TRANCOUNT", varname) == 0) || + (pg_strcasecmp("@@VERSION", varname) == 0) || + (pg_strcasecmp("@@MICROSOFTVERSION", varname) == 0) + ) + return true; + else + return false; +} + /*********************************************************************************** * FREE FUNCTIONS **********************************************************************************/ diff --git a/contrib/babelfishpg_tsql/src/pltsql.h b/contrib/babelfishpg_tsql/src/pltsql.h index 4868f81ac5..1dfe40f816 100644 --- a/contrib/babelfishpg_tsql/src/pltsql.h +++ b/contrib/babelfishpg_tsql/src/pltsql.h @@ -2127,6 +2127,8 @@ extern void pltsql_dumptree(PLtsql_function *func); extern void pre_function_call_hook_impl(const char *funcName); extern int32 coalesce_typmod_hook_impl(const CoalesceExpr *cexpr); extern void check_restricted_stored_procedure(Oid proc_id); +extern bool is_tsql_atatglobalvar(const char *varname); +extern bool is_tsql_atatuservar(const char *varname); /* * Scanner functions in pl_scanner.c diff --git a/contrib/babelfishpg_tsql/src/tsqlIface.cpp b/contrib/babelfishpg_tsql/src/tsqlIface.cpp index 073ea06b30..36a8ce4e22 100644 --- a/contrib/babelfishpg_tsql/src/tsqlIface.cpp +++ b/contrib/babelfishpg_tsql/src/tsqlIface.cpp @@ -217,6 +217,10 @@ static void handleOrderByOffsetFetch(TSqlParser::Order_by_clauseContext *ctx); static void rewrite_string_agg_query(TSqlParser::STRING_AGGContext *ctx); static bool setSysSchema = false; static void rewrite_function_trim_to_sys_trim(TSqlParser::TRIMContext *ctx); +static bool isAtAtUserVarName(const std::string name); +static bool isDelimitedAtAtUserVarName(const std::string name); +static void handleLocal_id(TSqlParser::Local_idContext *ctx, bool inSqlObject); +static std::string delimitIfAtAtUserVarName(const std::string name); /* * Structure / Utility function for general purpose of query string modification @@ -260,10 +264,13 @@ static void rewrite_function_trim_to_sys_trim(TSqlParser::TRIMContext *ctx); // global object is enough because no nesting is expected. static std::map> rewritten_query_fragment; -// Keeping poisitions of local_ids to quote them. -// local_id can be rewritten in differeny way in some cases (itvf), don't use rewritten_query_fragment. +// Keeping positions of local_ids to quote them. +// local_id can be rewritten in different ways in some cases (itvf), don't use rewritten_query_fragment. // TODO: incorporate local_id_positions with rewritten_query_fragment static std::map local_id_positions; + +// For user-defined variables like @@var or @var# in the RETURN clause of an ITVF +static std::map local_id_positions_atatuservar; // should be called before visiting subclause to make PLtsql_stmt. static void clear_rewritten_query_fragment(); @@ -489,10 +496,11 @@ attachPLtsql_fragment(ParseTree *node, PLtsql_stmt *fragment) { if (fragment) { - const char *tsqlDesc = pltsql_stmt_typename(fragment); - if (pltsql_enable_antlr_detailed_log) + { + const char *tsqlDesc = pltsql_stmt_typename(fragment); std::cout << " attachPLtsql_fragment(" << (void *) node << ", " << fragment << "[" << tsqlDesc << "])" << std::endl; + } fragments.put(node, fragment); } else @@ -1410,6 +1418,7 @@ class tsqlBuilder : public tsqlCommonMutator std::string db_name; bool is_function = false; bool is_schema_specified = false; + bool in_func_body_return_select_body = false; // We keep a stack of the containers that are active during a traversal. // A container will correspond to a block or a batch - these are containers @@ -1935,8 +1944,13 @@ class tsqlBuilder : public tsqlCommonMutator throw PGErrorWrapperException(ERROR, ERRCODE_INVALID_FUNCTION_DEFINITION, "'INSERT EXEC' cannot be used within a function", getLineAndPos(ddl_object)); } else if (ddl_object && !ddl_object->local_id()) /* insert into non-local object */ - { - throw PGErrorWrapperException(ERROR, ERRCODE_INVALID_FUNCTION_DEFINITION, "'INSERT' cannot be used within a function", getLineAndPos(ddl_object)); + { + if (ddl_object && ddl_object->full_object_name() && isDelimitedAtAtUserVarName(getFullText(ddl_object->full_object_name()) )) + { + /* This is a table variable name enclosed in delimiters, which is OK */ + } + else + throw PGErrorWrapperException(ERROR, ERRCODE_INVALID_FUNCTION_DEFINITION, "'INSERT' cannot be used within a function", getLineAndPos(ddl_object)); } } else if (ctx->update_statement() || ctx->delete_statement()) @@ -1967,16 +1981,20 @@ class tsqlBuilder : public tsqlCommonMutator } else if (ddl_object && !ddl_object->local_id()) { - if (ddl_object && ddl_object->full_object_name()) + if (ddl_object && ddl_object->full_object_name() && isDelimitedAtAtUserVarName(getFullText(ddl_object->full_object_name()) )) { - // DML target can be an alias: verify that the alias is for a table variable - if (table_sources) + /* DML target is a table variable name enclosed in delimiters, which is OK */ + dmlTargetAllowed = true; + } + else + { + if (ddl_object && ddl_object->full_object_name()) { + /* DML target can be an alias: verify that the alias is for a table variable */ line_and_pos = getLineAndPos(ddl_object); - // DML target can be an alias: verify that the alias is for a table variable if (table_sources) { - line_and_pos = getLineAndPos(ddl_object->full_object_name()); + line_and_pos = getLineAndPos(ddl_object->full_object_name()); std::string dmlTarget = getFullText(ddl_object->full_object_name()); dmlTargetAllowed = dmlTargetIsTabvar(table_sources->table_source_item(), dmlTarget); } @@ -2057,7 +2075,13 @@ class tsqlBuilder : public tsqlCommonMutator if (table_source_item[i]->local_id()) { - // Table variable, with alias matching the DML target + // Table variable name, with alias matching the DML target + return true; + } + + if (isDelimitedAtAtUserVarName(getFullText(table_source_item[i]->full_object_name()))) + { + // Delimited table variable name, with alias matching the DML target return true; } } @@ -2202,7 +2226,12 @@ class tsqlBuilder : public tsqlCommonMutator PLtsql_stmt_print *stmt = (PLtsql_stmt_print *) getPLtsql_fragment(ctx); expr = (PLtsql_expr *) linitial(stmt->exprs); } - else if (ctx->raiseerror_statement() && ctx->raiseerror_statement()->raiseerror_msg() && ctx->raiseerror_statement()->raiseerror_msg()->char_string()) + else if (ctx->raiseerror_statement() && + ctx->raiseerror_statement()->raiseerror_msg() && + (ctx->raiseerror_statement()->raiseerror_msg()->char_string() || + ctx->raiseerror_statement()->raiseerror_msg()->local_id() + ) + ) { PLtsql_stmt_raiserror *stmt = (PLtsql_stmt_raiserror *) getPLtsql_fragment(ctx); expr = (PLtsql_expr *) linitial(stmt->params); @@ -2358,6 +2387,14 @@ class tsqlBuilder : public tsqlCommonMutator add_rewritten_query_fragment_to_mutator(&mutator); // move information of rewritten_query_fragment to mutator. mutator.run(); // expr->query will be rewitten here } + else if (stmt->cmd_type == PLTSQL_STMT_EXEC_BATCH) + { + PLtsql_stmt_exec_batch *stmtExec = (PLtsql_stmt_exec_batch *) getPLtsql_fragment(ctx); + PLtsql_expr_query_mutator mutator(stmtExec->expr, ctx); + mutator.markSelectFragment(ctx); + add_rewritten_query_fragment_to_mutator(&mutator); + mutator.run(); + } } else if (ctx->set_statement() && ctx->set_statement()->expression()) @@ -2371,7 +2408,17 @@ class tsqlBuilder : public tsqlCommonMutator add_rewritten_query_fragment_to_mutator(&mutator); mutator.run(); } - + else if (ctx->set_statement() && + ctx->set_statement()->set_special() && + ctx->set_statement()->set_special()->constant_LOCAL_ID() && + ctx->set_statement()->set_special()->constant_LOCAL_ID()->local_id()) + { + PLtsql_stmt_execsql *stmt = (PLtsql_stmt_execsql *) getPLtsql_fragment(ctx->set_statement()); + PLtsql_expr_query_mutator mutator(stmt->sqlstmt, ctx->set_statement()); + add_rewritten_query_fragment_to_mutator(&mutator); + mutator.run(); + } + else if (ctx->declare_statement()) { if (ctx->declare_statement()->declare_local().size() > 0) @@ -2399,10 +2446,17 @@ class tsqlBuilder : public tsqlCommonMutator } } } + else if (ctx->transaction_statement()) + { + PLtsql_stmt_execsql *stmt = (PLtsql_stmt_execsql *) getPLtsql_fragment(ctx); + PLtsql_expr_query_mutator mutator(stmt->sqlstmt, ctx); + add_rewritten_query_fragment_to_mutator(&mutator); + mutator.run(); + } // remove the offsets for processed fragments - selectFragmentOffsets.clear(); - + selectFragmentOffsets.clear(); + clear_rewritten_query_fragment(); } @@ -2584,20 +2638,25 @@ class tsqlBuilder : public tsqlCommonMutator ////////////////////////////////////////////////////////////////////////////// void enterLocal_id(TSqlParser::Local_idContext *ctx) override { - std::string local_id_str = ::getFullText(ctx); + std::string local_id_str = getFullText(ctx); if (local_id_str.length() > 2 && local_id_str[0] == '@' && local_id_str[1] == '@') { - // starting with "@@" is a global variable (or internal function). skip adding. + // Starting with '@@': this is a T-SQL global variable; these are handled separately return; } - // keep to add quote later + // Keep start position of the to add quotes around the vriable name later if (ctx->start) { local_id_positions.emplace(std::make_pair(ctx->start->getStartIndex(), local_id_str)); } } + void exitLocal_id(TSqlParser::Local_idContext *ctx) override + { + handleLocal_id(ctx, in_create_or_alter_procedure || in_create_or_alter_trigger); + } + void exitFull_object_name(TSqlParser::Full_object_nameContext *ctx) override { if (ctx && (ctx->DOT().size() <= 2) && ctx->schema) @@ -2646,6 +2705,20 @@ class tsqlBuilder : public tsqlCommonMutator void exitFull_column_name(TSqlParser::Full_column_nameContext *ctx) override { handleFullColumnNameCtx(ctx); + + if (in_func_body_return_select_body) + { + if (ctx->DOT().empty()) + { + std::string local_id_str = getFullText(ctx); + if (isDelimitedAtAtUserVarName(local_id_str)) { + // We're in the RETURN clause of an ITVF, and any variables names @@var or @var# will be delimited at this point. + // Downstream these need to be temporarily replaced with CAST(NULL as ), and to make that work we need + // to record their positions here. + local_id_positions_atatuservar.emplace(std::make_pair(ctx->start->getStartIndex(), local_id_str)); + } + } + } } /* We are adding handling for CLR_UDT Types in: @@ -2824,31 +2897,35 @@ class tsqlBuilder : public tsqlCommonMutator { // prepare rewriting clear_rewritten_query_fragment(); + local_id_positions_atatuservar.clear(); + in_func_body_return_select_body = true; } void exitFunc_body_return_select_body(TSqlParser::Func_body_return_select_bodyContext *ctx) override { handleITVFBody(ctx); clear_rewritten_query_fragment(); + local_id_positions_atatuservar.clear(); + in_func_body_return_select_body = false; } void enterExecute_body_batch(TSqlParser::Execute_body_batchContext *ctx) override { - in_execute_body_batch = true; + in_execute_body_batch = true; PLtsql_stmt *stmt = makeExecBodyBatch(ctx); attachPLtsql_fragment(ctx, stmt); graft(stmt, peekContainer()); - clear_rewritten_query_fragment(); + clear_rewritten_query_fragment(); } void exitExecute_body_batch(TSqlParser::Execute_body_batchContext *ctx) override { - in_execute_body_batch = false; + in_execute_body_batch = false; PLtsql_stmt_exec *stmt = (PLtsql_stmt_exec *) getPLtsql_fragment(ctx); PLtsql_expr_query_mutator mutator(stmt->expr, ctx); add_rewritten_query_fragment_to_mutator(&mutator); mutator.run(); - clear_rewritten_query_fragment(); + clear_rewritten_query_fragment(); } PLtsql_expr *rewrite_if_condition(TSqlParser::Search_conditionContext *ctx) @@ -2895,10 +2972,13 @@ class tsqlBuilder : public tsqlCommonMutator class tsqlMutator : public TSqlParserBaseListener { public: - const std::vector &ruleNames; - MyInputStream &stream; - bool in_procedure_parameter = false; - bool in_procedure_parameter_id = false; + const std::vector &ruleNames; + MyInputStream &stream; + bool in_procedure_parameter = false; + bool in_procedure_parameter_id = false; + bool in_func_body_return_select_body = false; + bool in_create_or_alter_procedure = false; + bool in_create_or_alter_trigger = false; std::vector double_quota_places; @@ -2956,6 +3036,22 @@ class tsqlMutator : public TSqlParserBaseListener return result; } + void enterCreate_or_alter_procedure(TSqlParser::Create_or_alter_procedureContext *ctx) override { + in_create_or_alter_procedure = true; + } + + void exitCreate_or_alter_procedure(TSqlParser::Create_or_alter_procedureContext *ctx) override { + in_create_or_alter_procedure = false; + } + + void enterCreate_or_alter_trigger(TSqlParser::Create_or_alter_triggerContext *ctx) override { + in_create_or_alter_trigger = true; + } + + void exitCreate_or_alter_trigger(TSqlParser::Create_or_alter_triggerContext *ctx) override { + in_create_or_alter_trigger = false; + } + void enterComparison_operator(TSqlParser::Comparison_operatorContext *ctx) override { // Handle multiple cases: @@ -3006,8 +3102,18 @@ class tsqlMutator : public TSqlParserBaseListener if (pltsql_enable_antlr_detailed_log) std::cout << "-leaving (tsqlMutator)" << (void *) ctx << "[" << desc << "]" << std::endl; - } + } + + void enterFunc_body_return_select_body(TSqlParser::Func_body_return_select_bodyContext *ctx) override + { + in_func_body_return_select_body = true; + } + void exitFunc_body_return_select_body(TSqlParser::Func_body_return_select_bodyContext *ctx) override + { + in_func_body_return_select_body = false; + } + void enterFunc_proc_name_server_database_schema(TSqlParser::Func_proc_name_server_database_schemaContext *ctx) override { // We are looking at a function name; it may be a function call, or a @@ -3148,16 +3254,21 @@ class tsqlMutator : public TSqlParserBaseListener in_procedure_parameter = true; } } - + void enterFull_column_name(TSqlParser::Full_column_nameContext *ctx) override { if (in_procedure_parameter) { in_procedure_parameter_id = true; - } + } + } + + void exitLocal_id(TSqlParser::Local_idContext *ctx) override + { + handleLocal_id(ctx, in_create_or_alter_procedure || in_create_or_alter_trigger); } void exitFunc_body_returns_scalar(TSqlParser::Func_body_returns_scalarContext *ctx) override - { + { // If no AS keyword is specified, insert it prior to the BEGIN keyword. // This only applies to scalar functions; for other function types, the optional AS keyword // is already supported. @@ -4571,16 +4682,16 @@ makeTsqlExpr(const std::string &fragment, bool addSelect) PLtsql_expr *result = (PLtsql_expr *) palloc0(sizeof(*result)); if (addSelect) - result->query = pstrdup((fragment_SELECT_prefix + fragment).c_str()); + result->query = pstrdup((fragment_SELECT_prefix + delimitIfAtAtUserVarName(fragment)).c_str()); else - result->query = pstrdup(fragment.c_str()); - - result->plan = NULL; - result->paramnos = NULL; - result->rwparam = -1; - result->ns = pltsql_ns_top(); - - return result; + result->query = pstrdup(delimitIfAtAtUserVarName(fragment).c_str()); + + result->plan = NULL; + result->paramnos = NULL; + result->rwparam = -1; + result->ns = pltsql_ns_top(); + + return result; } PLtsql_expr * @@ -5087,19 +5198,32 @@ makeReturnQueryStmt(TSqlParser::Select_statement_standaloneContext *ctx, bool it add_rewritten_query_fragment_to_mutator(&itvf_mutator); std::u32string query = utf8_to_utf32(itvf_expr->query); + + /* + * Add the positions of @@var / @var# names to make the logic below work + * for getting the column list. + */ + for (const auto &entryA : local_id_positions_atatuservar) + { + std::string varName = delimitIfAtAtUserVarName(entryA.second); + local_id_positions.emplace(std::make_pair(entryA.first, varName)); + } + for (const auto &entry : local_id_positions) { const std::string& local_id = entry.second; + std::string local_id_lookup = local_id; + if (isDelimitedAtAtUserVarName(local_id)) local_id_lookup = local_id.substr(1,local_id.length()-2); const std::u32string& local_id_u32 = utf8_to_utf32(local_id.c_str()); size_t offset = entry.first - base_index; if (query.substr(offset, local_id_u32.length()) == local_id_u32) // local_id maybe already deleted in some cases such as select-assignment. check here if it still exists) { int dno; - PLtsql_nsitem *nse = pltsql_ns_lookup(pltsql_ns_top(), false, local_id.c_str(), nullptr, nullptr, nullptr); + PLtsql_nsitem *nse = pltsql_ns_lookup(pltsql_ns_top(), false, local_id_lookup.c_str(), nullptr, nullptr, nullptr); if (nse) dno = nse->itemno; else - throw PGErrorWrapperException(ERROR, ERRCODE_SYNTAX_ERROR, format_errmsg("\"%s\" is not a known variable", local_id.c_str()), getLineAndPos(ctx)); + throw PGErrorWrapperException(ERROR, ERRCODE_SYNTAX_ERROR, format_errmsg("\"%s\" is not a known variable", local_id_lookup.c_str()), getLineAndPos(ctx)); PLtsql_var *var = (PLtsql_var *) pltsql_Datums[dno]; std::string repl_text = std::string("CAST(NULL AS ") + std::string(var->datatype->typname) + std::string(")"); @@ -5188,7 +5312,7 @@ makeRaiseErrorStmt(TSqlParser::Raiseerror_statementContext *ctx) result->seterror = false; // msg, severity, state - result->params = lappend(result->params, makeTsqlExpr(ctx->msg->getText(), true)); + result->params = lappend(result->params, makeTsqlExpr(delimitIfAtAtUserVarName(ctx->msg->getText()), true)); recordSelectFragmentOffsets(ctx->parent, ctx->raiseerror_msg()); result->params = lappend(result->params, makeTsqlExpr(ctx->severity, true)); @@ -5269,10 +5393,10 @@ makeDeclareStmt(TSqlParser::Declare_statementContext *ctx, std::mapLOCAL_ID() && ctx->table_type_definition()) + if (ctx->local_id() && ctx->table_type_definition()) { - std::string nameStr = ::getFullText(ctx->LOCAL_ID()); - std::string typeStr = ::getFullText(ctx->table_type_definition()); + std::string nameStr = getFullText(ctx->local_id()); + std::string typeStr = getFullText(ctx->table_type_definition()); const char *name = downcase_truncate_identifier(nameStr.c_str(), nameStr.length(), true); check_dup_declare(name); PLtsql_type *type = parse_datatype(typeStr.c_str(), 0); @@ -5287,7 +5411,7 @@ makeDeclareStmt(TSqlParser::Declare_statementContext *ctx, std::mapLOCAL_ID()); + std::string nameStr = getFullText(local->LOCAL_ID()); const char *name = downcase_truncate_identifier(nameStr.c_str(), nameStr.length(), true); check_dup_declare(name); @@ -5417,7 +5541,7 @@ makeSQL(ParserRuleContext *ctx) static bool is_valid_set_option(std::string val) { - /* ON/OFF option and other special options (i.e. TRANSACTION ISOLATION LEVEL) are not incldued in this function because they are handled by grammar */ + /* ON/OFF option and other special options (i.e. TRANSACTION ISOLATION LEVEL) are not included in this function because they are handled by grammar */ return (pg_strcasecmp("DATEFIRST", val.c_str()) == 0) || (pg_strcasecmp("DATEFORMAT", val.c_str()) == 0) || (pg_strcasecmp("DEADLOCK_PRIORITY", val.c_str()) == 0) || @@ -5654,7 +5778,7 @@ makeSetStatement(TSqlParser::Set_statementContext *ctx, tsqlBuilder &builder) PLtsql_stmt_execsql *stmt = (PLtsql_stmt_execsql *) palloc0(sizeof(PLtsql_stmt_execsql)); std::string query; - query += "CALL bbf_set_context_info(convert(varbinary(128), "; + query += "CALL sys.bbf_set_context_info(convert(varbinary(128), "; query += param; query += "));"; @@ -5672,7 +5796,10 @@ makeSetStatement(TSqlParser::Set_statementContext *ctx, tsqlBuilder &builder) return (PLtsql_stmt *) stmt; } else + { + // We get here for other SET options that do not fall under set_on_off_option or special_variable, like DATEFORMAT return makeSQL(ctx); + } } else if (set_special_ctx->OFFSETS()) return nullptr; @@ -5697,7 +5824,7 @@ makeSetStatement(TSqlParser::Set_statementContext *ctx, tsqlBuilder &builder) { TSqlParser::Special_variableContext *guc_ctx = static_cast (set_special_ctx->special_variable()); /* build expression with the input variable */ - PLtsql_expr* input_expr = makeTsqlExpr(getFullText(set_special_ctx->LOCAL_ID()), true); + PLtsql_expr* input_expr = makeTsqlExpr(delimitIfAtAtUserVarName(getFullText(set_special_ctx->LOCAL_ID())), true); /* build target variable for this GUC, so that in backend we can identify that target is GUC */ PLtsql_var *target_var = build_babelfish_guc_variable(guc_ctx); /* assign expression to target */ @@ -5709,7 +5836,9 @@ makeSetStatement(TSqlParser::Set_statementContext *ctx, tsqlBuilder &builder) return (PLtsql_stmt *) result; } else + { return makeSQL(ctx); + } } else return nullptr; @@ -5892,11 +6021,15 @@ makeExecuteStatement(TSqlParser::Execute_statementContext *ctx) } std::string expr_query = ss.str(); result->expr = makeTsqlExpr(expr_query, true); - + if (!exec_strings.empty()) + { + recordSelectFragmentOffsets(ctx->parent, exec_strings[0]->getStart()->getStartIndex(), exec_strings[exec_strings.size()-1]->getStop()->getStopIndex(), 0); + } + return (PLtsql_stmt *) result; } else /* execute a stored procedure or function */ - { + { return makeExecuteProcedure(ctx, "execute_statement"); } } @@ -6055,8 +6188,9 @@ makeFetchCursorStatement(TSqlParser::Fetch_cursorContext *ctx) for (size_t i=0; iitemtype == PLTSQL_NSTYPE_REC || @@ -6382,7 +6516,7 @@ makeTransactionStatement(TSqlParser::Transaction_statementContext *ctx) PLtsql_stmt_execsql *stmt = (PLtsql_stmt_execsql *) result; stmt->txn_data = (PLtsql_txn_data *) palloc0(sizeof(PLtsql_txn_data)); - auto *localID = ctx->LOCAL_ID(); + auto *localID = ctx->local_id(); if (localID) { stmt->txn_data->txn_name_expr = makeTsqlExpr(::getFullText(localID), true); @@ -6428,7 +6562,7 @@ makeAnother(TSqlParser::Another_statementContext *ctx, tsqlBuilder &builder) for (PLtsql_stmt *stmt : result) { - // Associate each fragement with a tree node + // Associate each fragment with a tree node if (!declare_local_expr.empty() && declare_local_expr.find(stmt) != declare_local_expr.end()) { attachPLtsql_fragment(declare_local_expr.at(stmt), stmt); @@ -6474,7 +6608,7 @@ makeExecuteProcedure(ParserRuleContext *ctx, std::string call_type) std::string execKeywd = "EXEC"; // DO NOT CHANGE! int name_length = 0; - // Use a boolean vor convenience + // Use a boolean for convenience bool execute_statement = string_matches(call_type.c_str(), "execute_statement") ? true : false; size_t startPos = ctx->start->getStartIndex(); // start position of statement @@ -6725,29 +6859,47 @@ makeDbccCheckidentStatement(TSqlParser::Dbcc_statementContext *ctx) stmt->cmd_type = PLTSQL_STMT_DBCC; stmt->dbcc_stmt_type = PLTSQL_DBCC_CHECKIDENT; - if (ctx->table_name_string()) + /* + * FIXME: the table name and new value arguments can be local variables. The grammar supports this but + * the processing below does not yet. These cases are currently caught in TsqlUnsupportedFeatureHandler + * and a polite error message is raised. Consequently, we should never get here for those cases, and + * for clarity, the two Assert(0) statements are marking those locations which we should never arrive at. + */ + if (ctx->dbcc_table_name()) { - if(ctx->table_name_string()->table) + if (ctx->dbcc_table_name()->table) { - input_str = stripQuoteFromId(ctx->table_name_string()->table); + input_str = stripQuoteFromId(ctx->dbcc_table_name()->table); } - if (ctx->table_name_string()->char_string()) + else if (ctx->dbcc_table_name()->char_string()) { - input_str = ctx->table_name_string()->char_string()->STRING()->getSymbol()->getText(); + input_str = ctx->dbcc_table_name()->char_string()->STRING()->getSymbol()->getText(); if (input_str.length() <= 2) throw PGErrorWrapperException(ERROR, ERRCODE_INVALID_PARAMETER_VALUE, "Parameter 1 is incorrect for this DBCC statement", - getLineAndPos(ctx->table_name_string())); + getLineAndPos(ctx->dbcc_table_name())); input_str = input_str.substr(1, input_str.length()-2); } + else if (ctx->dbcc_table_name()->local_id()) + { + // This is already caught in TsqlUnsupportedFeatureHandler, so we should not get here + Assert(0); + } + if (ctx->RESEED()) { - if (ctx->new_value) + if (ctx->checkident_new_value()) { - if(ctx->MINUS()) - stmt->dbcc_stmt_data.dbcc_checkident.new_reseed_value = pstrdup((ctx->new_value->getText().insert(0,"-")).c_str()); + if (ctx->checkident_new_value()->local_id()) + { + // This is already caught in TsqlUnsupportedFeatureHandler, so we should not get here + Assert(0); + } + + if (ctx->MINUS()) + stmt->dbcc_stmt_data.dbcc_checkident.new_reseed_value = pstrdup((ctx->checkident_new_value()->getText().insert(0,"-")).c_str()); else - stmt->dbcc_stmt_data.dbcc_checkident.new_reseed_value = pstrdup((ctx->new_value->getText()).c_str()); + stmt->dbcc_stmt_data.dbcc_checkident.new_reseed_value = pstrdup((ctx->checkident_new_value()->getText()).c_str()); } } else if (ctx->NORESEED()) @@ -6827,8 +6979,9 @@ create_select_target_row(const char *refname, size_t nfields, int lineno) // Add target column to target row for assignment void add_assignment_target_field(PLtsql_row *target, antlr4::tree::TerminalNode *localId, size_t idx) { - auto targetText = ::getFullText(localId); - PLtsql_nsitem *nse = pltsql_ns_lookup(pltsql_ns_top(), false, targetText.c_str(), nullptr, nullptr, nullptr); + std::string targetText = getFullText(localId); + const char *nameLookup = downcase_truncate_identifier(targetText.c_str(), targetText.length(), true); + PLtsql_nsitem *nse = pltsql_ns_lookup(pltsql_ns_top(), false, nameLookup, nullptr, nullptr, nullptr); if (!nse) throw PGErrorWrapperException(ERROR, ERRCODE_SYNTAX_ERROR, format_errmsg("\"%s\" is not a known variable", targetText.c_str()), getLineAndPos(localId)); @@ -7769,7 +7922,8 @@ post_process_declare_cursor_statement(PLtsql_stmt_decl_cursor *stmt, TSqlParser: static PLtsql_var * lookup_cursor_variable(const char *varname) { - PLtsql_nsitem *nse = pltsql_ns_lookup(pltsql_ns_top(), false, varname, nullptr, nullptr, nullptr); + const char *varnameLookup = downcase_truncate_identifier(varname, strlen(varname), true); + PLtsql_nsitem *nse = pltsql_ns_lookup(pltsql_ns_top(), false, varnameLookup, nullptr, nullptr, nullptr); if (!nse) throw PGErrorWrapperException(ERROR, ERRCODE_SYNTAX_ERROR, format_errmsg("\"%s\" is not a known variable", varname), 0, 0); @@ -8094,23 +8248,25 @@ static tsql_exec_param * makeSpParam(TSqlParser::Execute_statement_arg_namedContext *ctx) { TSqlParser::Execute_parameterContext *exec_param = ctx->execute_parameter(); - Assert(exec_param && ctx->LOCAL_ID()); + Assert(exec_param && ctx->local_id()); tsql_exec_param *p = (tsql_exec_param *) palloc0(sizeof(*p)); - auto targetText = ::getFullText(ctx->LOCAL_ID()); + std::string targetText = ::getFullText(ctx->local_id()); p->name = pstrdup(targetText.c_str()); p->varno = -1; p->mode = FUNC_PARAM_IN; - if (exec_param->LOCAL_ID() && (exec_param->OUTPUT() || exec_param->OUT())) + if (exec_param->local_id() && (exec_param->OUTPUT() || exec_param->OUT())) { - auto *localID = exec_param->LOCAL_ID(); + auto *localID = exec_param->local_id()->LOCAL_ID(); p->varno = getVarno(localID); p->expr = makeTsqlExpr(::getFullText(localID), true); p->mode = FUNC_PARAM_INOUT; } else + { p->expr = makeTsqlExpr(exec_param, true); + } return p; } @@ -8126,15 +8282,17 @@ makeSpParam(TSqlParser::Execute_statement_arg_unnamedContext *ctx) p->varno = -1; p->mode = FUNC_PARAM_IN; - if (exec_param->LOCAL_ID() && (exec_param->OUTPUT() || exec_param->OUT())) + if (exec_param->local_id() && (exec_param->OUTPUT() || exec_param->OUT())) { - auto *localID = exec_param->LOCAL_ID(); + auto *localID = exec_param->local_id()->LOCAL_ID(); p->varno = getVarno(localID); p->expr = makeTsqlExpr(::getFullText(localID), true); p->mode = FUNC_PARAM_INOUT; } else + { p->expr = makeTsqlExpr(exec_param, true); + } return p; } @@ -8143,9 +8301,9 @@ static int getVarno(tree::TerminalNode *localID) { int dno = -1; - auto targetText = ::getFullText(localID); - - PLtsql_nsitem *nse = pltsql_ns_lookup(pltsql_ns_top(), false, targetText.c_str(), nullptr, nullptr, nullptr); + std::string targetText = getFullText(localID); + const char *nameLookup = downcase_truncate_identifier(targetText.c_str(), targetText.length(), true); + PLtsql_nsitem *nse = pltsql_ns_lookup(pltsql_ns_top(), false, nameLookup, nullptr, nullptr, nullptr); if (nse) dno = nse->itemno; @@ -9350,7 +9508,7 @@ handleOrderByOffsetFetch(TSqlParser::Order_by_clauseContext *ctx) // Do not rewrite the entire expression since that will break the logic in the mutator when there is something inside the // expression that also needs rewriting (like a local variable @p which needs to be rewritten as "@p"). // Instead, insert an opening and closing bracket in the right places. - // Also, do not add a rewrite at the start position of the expression since there may be an '@' for a local var + // Also, do not add a rewrite at the start position of the expression since there may be an '@' for a local var // at that position and the rewrite to double-quote the variable will be lost as a result. rewritten_query_fragment.emplace(std::make_pair((ctx->offset_exp->start->getStartIndex() - 1), std::make_pair("", " ("))); rewritten_query_fragment.emplace(std::make_pair((ctx->offset_exp->stop->getStopIndex() + 1), std::make_pair("", ") "))); @@ -9365,3 +9523,121 @@ handleOrderByOffsetFetch(TSqlParser::Order_by_clauseContext *ctx) return; } + +// Determine if a variable name contains a hash or starts with @@: these need special handling +// since the PG backend isn't able to process these names otherwise. +// For predefined global @@variables, these should not be delimited as the backend handles these already. +static bool +isAtAtUserVarName(const std::string name) +{ + if (name.length() >= 3) + { + if (name.front() == '@') + { + if ( + (name.find_first_of("#") != std::string::npos) || + (name[1] == '@') + ) + { + if (name.find_first_not_of("@#$_0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ") == std::string::npos) + { + if (!is_tsql_atatglobalvar(name.c_str())) + { + return true; + } + } + } + } + } + return false; +} + +// Check for same variable name as above, but delimited +static bool +isDelimitedAtAtUserVarName(const std::string name) +{ + // Check for delimiters. Both square brackets and double quotes are used as delimiters for variable names. + if (((name.front() == '[') && (name.back() == ']')) || ((name.front() == '"') && (name.back() == '"'))) + { + if (isAtAtUserVarName(name.substr(1,name.length()-2))) + { + return true; + } + } + return false; +} + +// Set up rewrites for variables names containing a hash or starting with @@ +static void +handleLocal_id(TSqlParser::Local_idContext *ctx, bool inSqlObject) +{ + std::string local_id = ::getFullText(ctx); + + // Don't do anything for a predefined T-SQL global @@variable + if (!isAtAtUserVarName(local_id)) + { + return; + } + + // When this is for a table variable declaration, do not add delimiters at this point since internally the nestlevel will be attached first + Assert(ctx->parent); + auto parentCtx = ctx->parent; + if (dynamic_cast(parentCtx)) + { + TSqlParser::Declare_statementContext *declareCtx = static_cast(parentCtx); + if (declareCtx->table_type_definition() != nullptr) + { + return; + } + } + + // For some cases, an @@var or @var# name should not be delimited when it is referenced inside a procedure or trigger, + // as it will be delimited once the body is executed. + if (inSqlObject) + { + if (dynamic_cast(parentCtx)) + { + return; + } + if (dynamic_cast(parentCtx)) + { + return; + } + if (dynamic_cast(parentCtx)) + { + return; + } + if (dynamic_cast(parentCtx)) + { + return; + } + if (dynamic_cast(parentCtx)) + { + return; + } + if (dynamic_cast(parentCtx)) + { + Assert(parentCtx->parent); + if (dynamic_cast(parentCtx->parent)) + { + return; + } + } + } + + // Delimit a variable by rewriting. + // Using double quotes causes SELECT @@V to generate '@@V' as the result, so using square brackets instead. + rewritten_query_fragment.emplace(std::make_pair(ctx->start->getStartIndex(), std::make_pair(local_id, "[" + local_id + "]"))); +} + +// Delimit a variable name directly (rather than by rewriting) +static std::string +delimitIfAtAtUserVarName(const std::string name) +{ + std::string str = name; + if (isAtAtUserVarName(name)) + { + str = "[" + str + "]"; + } + return str; +} diff --git a/contrib/babelfishpg_tsql/src/tsqlUnsupportedFeatureHandler.cpp b/contrib/babelfishpg_tsql/src/tsqlUnsupportedFeatureHandler.cpp index 097444d5fe..fd86e02bc9 100644 --- a/contrib/babelfishpg_tsql/src/tsqlUnsupportedFeatureHandler.cpp +++ b/contrib/babelfishpg_tsql/src/tsqlUnsupportedFeatureHandler.cpp @@ -1333,6 +1333,24 @@ antlrcpp::Any TsqlUnsupportedFeatureHandlerImpl::visitTransaction_statement(TSql antlrcpp::Any TsqlUnsupportedFeatureHandlerImpl::visitDbcc_statement(TSqlParser::Dbcc_statementContext *ctx) { + if (ctx->CHECKIDENT()) + { + Assert(ctx->dbcc_table_name()); + if (ctx->dbcc_table_name()->local_id()) + { + throw PGErrorWrapperException(ERROR, ERRCODE_SYNTAX_ERROR, + "DBCC CHECKIDENT does not currently support a variable for the table name", + getLineAndPos(ctx->dbcc_table_name())); + } + + if (ctx->checkident_new_value()) + { + if (ctx->checkident_new_value()->local_id()) + throw PGErrorWrapperException(ERROR, ERRCODE_SYNTAX_ERROR, + "DBCC CHECKIDENT does not currently support a variable for the RESEED value", + getLineAndPos(ctx->checkident_new_value())); + } + } if (ctx->dbcc_command()) { diff --git a/test/JDBC/expected/BABEL-3092.out b/test/JDBC/expected/BABEL-3092.out index ef55195463..748c1074f0 100644 --- a/test/JDBC/expected/BABEL-3092.out +++ b/test/JDBC/expected/BABEL-3092.out @@ -356,7 +356,7 @@ EXEC("SELECT * FROM t") GO ~~ERROR (Code: 33557097)~~ -~~ERROR (Message: column "select * from t" does not exist)~~ +~~ERROR (Message: relation "t" does not exist)~~ EXEC(@v) GO diff --git a/test/JDBC/expected/atatuservar-vu-cleanup.out b/test/JDBC/expected/atatuservar-vu-cleanup.out new file mode 100644 index 0000000000..3c57e1c16c --- /dev/null +++ b/test/JDBC/expected/atatuservar-vu-cleanup.out @@ -0,0 +1,167 @@ +drop procedure p1_atatuservar +go +drop procedure p2_atatuservar +go +drop function f1_atatuservar +go +drop function f2_atatuservar +go +drop procedure p3_atatuservar +go +drop function f3_atatuservar +go +drop procedure p4_atatuservar +go +drop procedure p19_atatuservar +go +drop procedure p5_atatuservar +go +drop procedure p6_atatuservar +go +drop procedure p7_atatuservar +go +drop procedure p8_atatuservar +go +drop procedure p9_atatuservar +go +drop procedure p10_atatuservar +go +drop procedure p11_atatuservar +go +drop function f4_atatuservar +go +drop procedure p22_atatuservar +go +drop procedure p12_atatuservar +go +drop procedure p13_atatuservar +go +drop procedure p14_atatuservar +go +drop function f1_atatuservar_extra_long_name_here +go +drop function f2_atatuservar_extra_long_name_here +go +drop procedure p15_atatuservar +go +drop procedure p16_atatuservar +go +drop procedure p17_atatuservar +go +drop procedure p18_atatuservar +go +drop procedure p1_raiserror_atatuservar +go +drop procedure p1_spexec_atatuservar +go +drop procedure p2_spexec_atatuservar +go +drop function f1_ins_atatuservar +go +drop function f1_upd_atatuservar +go +drop function f1_del_atatuservar +go +drop function f3_atatuservar_upd +go +drop function f3_atatuservar_del +go +drop function f3_upd_atatuservar +go +drop function f3_del_atatuservar +go +drop function f4_ins_atatuservar +go +drop function f4_upd_atatuservar +go +drop function f4_del_atatuservar +go +drop function f5_upd_atatuservar +go +drop function f5_del_atatuservar +go +drop function f6_upd_atatuservar +go +drop function f6_del_atatuservar +go +drop function f7_tabvar_udf_upd_atatuservar +go +drop function f9_tabvar_udf_del_atatuservar +go +drop function f10_tabvar_udf_del_atatuservar +go +drop function f4_atatuservar_tabvar_in_function_upd +go +drop function f14_del_atatuservar +go +drop function f15_upd_atatuservar +go +drop procedure p20_atatuservar +go +drop procedure p21_atatuservar +go +drop procedure p1_xactname_atatuservar +go +drop procedure p2_xactname_atatuservar +go + +drop table t1_atatuservar +go +drop table t2_atatuservar +go +drop table t3_atatuservar +go +drop table t_checkident_atatuservar +go +drop table t1_trigger_atatuservar +go +drop table t2_trigger_atatuservar +go +drop table t3_trigger_atatuservar +go +drop table t4_trigger_atatuservar +go +drop table t5_trigger_atatuservar +go +drop table t6_trigger_atatuservar +go +drop table t7_trigger_atatuservar +go +drop table t8_trigger_atatuservar +go +drop table t9_trigger_atatuservar +go +drop table t10_trigger_atatuservar +go +drop table t11_trigger_atatuservar +go +drop table t12_trigger_atatuservar +go +drop table t13_trigger_atatuservar +go +drop table t14_trigger_atatuservar +go +drop table t15_trigger_atatuservar +go +drop table t16_trigger_atatuservar +go +drop table t17_trigger_atatuservar +go +drop table t18_trigger_atatuservar +go +drop table t19_trigger_atatuservar +go +drop table t20_trigger_atatuservar +go +drop table t21_trigger_atatuservar +go +drop table t22_trigger_atatuservar +go +drop table t23_trigger_atatuservar +go +drop procedure p1_cursor_atatuservar +go +drop procedure p1_sysfunctions_atatuservar +go +drop function f1_sysfunctions_atatuservar +go diff --git a/test/JDBC/expected/atatuservar-vu-prepare.out b/test/JDBC/expected/atatuservar-vu-prepare.out new file mode 100644 index 0000000000..68eeafe85c --- /dev/null +++ b/test/JDBC/expected/atatuservar-vu-prepare.out @@ -0,0 +1,76 @@ +create table t1_atatuservar (c varchar(20)) +insert t1_atatuservar values ('row 1'), ('row 2'), ('row 3') +go +~~ROW COUNT: 3~~ + +create table t2_atatuservar (a int) +insert t2_atatuservar values(123) +go +~~ROW COUNT: 1~~ + +create table t3_atatuservar (a int) +insert t3_atatuservar values(123) +go +~~ROW COUNT: 1~~ + +create table t_checkident_atatuservar (a varchar(20), n int identity) +go +insert t_checkident_atatuservar values('one') +insert t_checkident_atatuservar values('two') +insert t_checkident_atatuservar values('three') +insert t_checkident_atatuservar values('four') +go +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +create table t1_trigger_atatuservar (a int) +go +create table t2_trigger_atatuservar (a int) +go +create table t3_trigger_atatuservar (a int) +go +create table t4_trigger_atatuservar (a int) +go +create table t5_trigger_atatuservar (a int) +go +create table t6_trigger_atatuservar (a int) +go +create table t7_trigger_atatuservar (a int) +go +create table t8_trigger_atatuservar (a int) +go +create table t9_trigger_atatuservar (a int) +go +create table t10_trigger_atatuservar(a int) +go +create table t11_trigger_atatuservar(a int) +go +create table t12_trigger_atatuservar(a int) +go +create table t13_trigger_atatuservar(a int) +go +create table t14_trigger_atatuservar(a int) +go +create table t15_trigger_atatuservar(a int) +go +create table t16_trigger_atatuservar(a int) +go +create table t17_trigger_atatuservar(a int) +go +create table t18_trigger_atatuservar(a int) +go +create table t19_trigger_atatuservar(a int) +go +create table t20_trigger_atatuservar(a int) +go +create table t21_trigger_atatuservar(a int) +go +create table t22_trigger_atatuservar(a int) +go +create table t23_trigger_atatuservar(a int) +go diff --git a/test/JDBC/expected/atatuservar-vu-verify.out b/test/JDBC/expected/atatuservar-vu-verify.out new file mode 100644 index 0000000000..81724023bf --- /dev/null +++ b/test/JDBC/expected/atatuservar-vu-verify.out @@ -0,0 +1,3856 @@ +-- simple variables in T-SQL batch +set quoted_identifier off +go +declare @@v int = 2 select @@v +go +~~START~~ +int +2 +~~END~~ + +declare @#v int = 2 select @#v +go +~~START~~ +int +2 +~~END~~ + +declare @@@$$@@@@v int = 2 select @@@$$@@@@v +go +~~START~~ +int +2 +~~END~~ + +declare @@@$$@@@@v##### int = 2 select @@@$$@@@@v##### +go +~~START~~ +int +2 +~~END~~ + +declare @@@$$@@@@#####v int = 2 select @@@$$@@@@#####v +go +~~START~~ +int +2 +~~END~~ + +declare @#############v int = 2 select @#############v +go +~~START~~ +int +2 +~~END~~ + + +-- 63 long +declare @@v63_7890123456789$123456789$123456789012345678901234567890123 int = 2 +select @@v63_7890123456789$123456789$123456789012345678901234567890123 +go +~~START~~ +int +2 +~~END~~ + +-- 64 long +declare @@v64_7890123456789$123456789$1234567890123456789012345678901234 int = 2 +select @@v64_7890123456789$123456789$1234567890123456789012345678901234 +go +~~START~~ +int +2 +~~END~~ + +-- maximum length in T-SQL is 128 +declare @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 int = 2 +select @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 +go +~~START~~ +int +2 +~~END~~ + + +declare @#v63_7890123456789$123456789$123456789012345678901234567890123 int = 2 +select @#v63_7890123456789$123456789$123456789012345678901234567890123 +go +~~START~~ +int +2 +~~END~~ + +declare @#v64_7890123456789$123456789$1234567890123456789012345678901234 int = 2 +select @#v64_7890123456789$123456789$1234567890123456789012345678901234 +go +~~START~~ +int +2 +~~END~~ + +declare @#v128_90123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 int = 2 +select @#v128_90123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 +go +~~START~~ +int +2 +~~END~~ + +declare @@v128_@@@$$@@@@$$@########@@@$$@@@@$$@@@@$$@@@###__##@@@$$@@@@$$@@@@$$@@@@$$@@@______@@@$$@@@@$$@@@@$$@@@###########@@@$$@@@@@@ int = 2 +select @@v128_@@@$$@@@@$$@########@@@$$@@@@$$@@@@$$@@@###__##@@@$$@@@@$$@@@@$$@@@@$$@@@______@@@$$@@@@$$@@@@$$@@@###########@@@$$@@@@@@ +go +~~START~~ +int +2 +~~END~~ + + +-- referencing global @@variable, in T-SQL batch, procedure, trigger and function +declare @@v int +select 1 +set @@v = @@rowcount +select @@v +go +~~START~~ +int +1 +~~END~~ + +~~START~~ +int +1 +~~END~~ + + +declare @@@$$@@@@v int, @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 int +select 1 +set @@@$$@@@@v = @@rowcount +set @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 = @@pgerror +select @@@$$@@@@v, @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 +go +~~START~~ +int +1 +~~END~~ + +~~START~~ +int#!#int +1#!#0 +~~END~~ + + +declare @@v63_7890123456789$123456789$123456789012345678901234567890123 int +set @@v63_7890123456789$123456789$123456789012345678901234567890123 = @@rowcount +select @@v63_7890123456789$123456789$123456789012345678901234567890123 +go +~~START~~ +int +1 +~~END~~ + + +declare @#v64_7890123456789$123456789$1234567890123456789012345678901234 int +set @#v64_7890123456789$123456789$1234567890123456789012345678901234 = @@rowcount +select @#v64_7890123456789$123456789$1234567890123456789012345678901234 +go +~~START~~ +int +1 +~~END~~ + + +declare @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 int +set @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 = @@rowcount +select @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 +go +~~START~~ +int +1 +~~END~~ + + + +create procedure p1_atatuservar +as +declare @@v int +select 1 +set @@v = @@rowcount +select @@v +declare @#v int +select 1 +set @#v = @@rowcount +select @#v +go +exec p1_atatuservar +go +~~START~~ +int +1 +~~END~~ + +~~START~~ +int +1 +~~END~~ + +~~START~~ +int +1 +~~END~~ + +~~START~~ +int +1 +~~END~~ + + + +create procedure p2_atatuservar +as +declare @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 int, @@p128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 int +select 1 +set @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 = @@rowcount +select @@p128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 = @@pgerror +select @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678, @@p128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 +declare @#v64_7890123456789$123456789$1234567890123456789012345678901234 int +select 1 +set @#v64_7890123456789$123456789$1234567890123456789012345678901234 = @@rowcount +select @#v64_7890123456789$123456789$1234567890123456789012345678901234 +go +exec p2_atatuservar +go +~~START~~ +int +1 +~~END~~ + +~~START~~ +int#!#int +1#!#0 +~~END~~ + +~~START~~ +int +1 +~~END~~ + +~~START~~ +int +1 +~~END~~ + + + +create trigger tr1_atatuservar on t1_trigger_atatuservar for insert +as +begin +select 'trigger tr1_atatuservar' +declare @@v int +select 1 +set @@v = @@rowcount +select @@v +declare @#v int +select 1 +set @#v = @@rowcount +select @#v +end +go +insert t1_trigger_atatuservar values (123) +go +~~START~~ +varchar +trigger tr1_atatuservar +~~END~~ + +~~START~~ +int +1 +~~END~~ + +~~START~~ +int +1 +~~END~~ + +~~START~~ +int +1 +~~END~~ + +~~START~~ +int +1 +~~END~~ + +~~ROW COUNT: 1~~ + + + + +create trigger tr2_atatuservar on t2_trigger_atatuservar for insert +as +begin +select 'trigger tr2_atatuservar' +declare @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 int +select 1 +set @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 = @@rowcount +select @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 +declare @#v64_7890123456789$123456789$1234567890123456789012345678901234 int +select 1 +set @#v64_7890123456789$123456789$1234567890123456789012345678901234 = @@rowcount +select @#v64_7890123456789$123456789$1234567890123456789012345678901234 +end +go +insert t2_trigger_atatuservar values (123) +go +~~START~~ +varchar +trigger tr2_atatuservar +~~END~~ + +~~START~~ +int +1 +~~END~~ + +~~START~~ +int +1 +~~END~~ + +~~START~~ +int +1 +~~END~~ + +~~START~~ +int +1 +~~END~~ + +~~ROW COUNT: 1~~ + + + + +create function f1_atatuservar() returns int +as +begin +declare @@v int, @@v2 int +select @@v2 = count(*) from t1_atatuservar +set @@v = @@rowcount +declare @#v int, @#v2 int +select @#v2 = count(*) from t1_atatuservar +set @#v = @@rowcount +return @@v * @#v +end +go +select dbo.f1_atatuservar() +go +~~START~~ +int +1 +~~END~~ + + + +create function f2_atatuservar() returns int +as +begin +declare @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 int, @#v64_7890123456789$123456789$1234567890123456789012345678901234 int +select @#v64_7890123456789$123456789$1234567890123456789012345678901234 = count(*) from t1_atatuservar +set @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 = @@rowcount +return @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 * @#v64_7890123456789$123456789$1234567890123456789012345678901234 +end +go +select dbo.f2_atatuservar() +go +~~START~~ +int +3 +~~END~~ + + + +create trigger tr3_atatuservar on t3_trigger_atatuservar for insert +as +begin +select 'trigger tr3_atatuservar' +declare @@v int, @@v2 int +select @@v2 = count(*) from t1_atatuservar +set @@v = @@rowcount +select @@v +declare @#v int, @#v2 int +select @#v2 = count(*) from t1_atatuservar +set @#v = @@rowcount +select @#v +end +go +insert t3_trigger_atatuservar values (123) +go +~~START~~ +varchar +trigger tr3_atatuservar +~~END~~ + +~~START~~ +int +1 +~~END~~ + +~~START~~ +int +1 +~~END~~ + +~~ROW COUNT: 1~~ + + +-- assuming @@servername is BABELFISH +declare @v varchar(50) +set @v = @@servername +select @v +go +~~START~~ +varchar +BABELFISH +~~END~~ + + +declare @v int +set @v = len(@@servername) +select @v +go +~~START~~ +int +9 +~~END~~ + + + +create procedure p3_atatuservar +as +declare @v varchar(50) +set @v = @@servername +select @v +declare @v2 int +set @v2 = len(@@servername) +select @v2 +go +exec p3_atatuservar +go +~~START~~ +varchar +BABELFISH +~~END~~ + +~~START~~ +int +9 +~~END~~ + + +create function f3_atatuservar(@#p1 varchar(30)) returns int +as +begin +declare @@v int +set @@v = len(@@servername) +return @@v * len(@#p1) +end +go +select dbo.f3_atatuservar(@@servername) +go +~~START~~ +int +81 +~~END~~ + + +create trigger tr4_atatuservar on t4_trigger_atatuservar for insert +as +begin +select 'trigger tr4_atatuservar' +declare @@v int +set @@v = len(@@servername) +select @@v +end +go +insert t4_trigger_atatuservar values (123) +go +~~START~~ +varchar +trigger tr4_atatuservar +~~END~~ + +~~START~~ +int +9 +~~END~~ + +~~ROW COUNT: 1~~ + + +-- procedure call with named/unnamed arguments, output parameters, return status, in T-SQL batch, procedure, trigger +create procedure p4_atatuservar @@p1 int +as select @@p1 +return @@p1*-1 +go +exec p4_atatuservar 123 +go +~~START~~ +int +123 +~~END~~ + +exec p4_atatuservar @@p1=123 +go +~~START~~ +int +123 +~~END~~ + +declare @@v int = 987 +exec p4_atatuservar @@p1=@@v +go +~~START~~ +int +987 +~~END~~ + +declare @@v int +exec @@v = p4_atatuservar @@p1=123 +select @@v +go +~~START~~ +int +123 +~~END~~ + +~~START~~ +int +-123 +~~END~~ + + +create procedure p19_atatuservar @#p19_atatuservar int +as select @#p19_atatuservar +return @#p19_atatuservar*-1 +go +exec p19_atatuservar @@max_precision +go +~~START~~ +int +38 +~~END~~ + +exec p19_atatuservar @#p19_atatuservar=@@max_precision +go +~~START~~ +int +38 +~~END~~ + +declare @#v int = @@max_precision +exec p19_atatuservar @#p19_atatuservar=@#v +go +~~START~~ +int +38 +~~END~~ + +declare @#v int +exec @#v = p19_atatuservar @#p19_atatuservar=@@max_precision +select @#v +go +~~START~~ +int +38 +~~END~~ + +~~START~~ +int +-38 +~~END~~ + + + +create procedure p5_atatuservar +as +declare @@v int +exec @@v = p4_atatuservar @@p1=123 +select @@v +declare @#v int +exec @#v = p4_atatuservar @@p1=123 +select @#v +go +exec p5_atatuservar +go +~~START~~ +int +123 +~~END~~ + +~~START~~ +int +-123 +~~END~~ + +~~START~~ +int +123 +~~END~~ + +~~START~~ +int +-123 +~~END~~ + + +create procedure p6_atatuservar @@p4 int output +as +set @@p4 *= 2 +select @@p4 +return @@p4*-1 +go +declare @#v1 int, @@v2 int = 123 +exec @#v1 = p6_atatuservar @@v2 +select @#v1, @@v2 +go +~~START~~ +int +246 +~~END~~ + +~~START~~ +int#!#int +-246#!#123 +~~END~~ + + +declare @#v1 int, @@v2 int = 123 +exec @#v1 = p6_atatuservar @@p4 = @@v2 out +select @#v1, @@v2 +go +~~START~~ +int +246 +~~END~~ + +~~START~~ +int#!#int +-246#!#246 +~~END~~ + + +create procedure p7_atatuservar +as +declare @#v1 int, @@v2 int = 123 +exec @#v1 = p6_atatuservar @@v2 output +select @#v1, @@v2 +go + +create procedure p8_atatuservar @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 int output +as +set @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 *= 2 +select @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 +return @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678*-1 +go +declare @#v64_7890123456789$123456789$1234567890123456789012345678901234 int, @@v63_7890123456789$123456789$123456789012345678901234567890123 int = 123 +exec @#v64_7890123456789$123456789$1234567890123456789012345678901234 = p8_atatuservar @@v63_7890123456789$123456789$123456789012345678901234567890123 +select @#v64_7890123456789$123456789$1234567890123456789012345678901234, @@v63_7890123456789$123456789$123456789012345678901234567890123 +go +~~START~~ +int +246 +~~END~~ + +~~START~~ +int#!#int +-246#!#123 +~~END~~ + + +declare @#v64_7890123456789$123456789$1234567890123456789012345678901234 int, @@v63_7890123456789$123456789$123456789012345678901234567890123 int = 123 +exec @#v64_7890123456789$123456789$1234567890123456789012345678901234 = p8_atatuservar @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 = @@v63_7890123456789$123456789$123456789012345678901234567890123 out +select @#v64_7890123456789$123456789$1234567890123456789012345678901234, @@v63_7890123456789$123456789$123456789012345678901234567890123 +go +~~START~~ +int +246 +~~END~~ + +~~START~~ +int#!#int +-246#!#246 +~~END~~ + + +create procedure p9_atatuservar +as +declare @#v64_7890123456789$123456789$1234567890123456789012345678901234 int, @@v63_7890123456789$123456789$123456789012345678901234567890123 int = 123 +exec @#v64_7890123456789$123456789$1234567890123456789012345678901234 = p8_atatuservar @@v63_7890123456789$123456789$123456789012345678901234567890123 output +select @#v64_7890123456789$123456789$1234567890123456789012345678901234, @@v63_7890123456789$123456789$123456789012345678901234567890123 +go + + +create trigger tr5_atatuservar on t5_trigger_atatuservar for insert +as +begin +select 'trigger tr5_atatuservar' +declare @@v int +exec @@v = p4_atatuservar @@p1=123 +select @@v +declare @#v int +exec @#v = p4_atatuservar @@p1=123 +select @#v +end +go +insert t5_trigger_atatuservar values (123) +go +~~START~~ +varchar +trigger tr5_atatuservar +~~END~~ + +~~START~~ +int +123 +~~END~~ + +~~START~~ +int +-123 +~~END~~ + +~~START~~ +int +123 +~~END~~ + +~~START~~ +int +-123 +~~END~~ + +~~ROW COUNT: 1~~ + + + +create trigger tr6_atatuservar on t6_trigger_atatuservar for insert +as +begin +select 'trigger tr6_atatuservar' +declare @#v64_7890123456789$123456789$1234567890123456789012345678901234 int, @@v63_7890123456789$123456789$123456789012345678901234567890123 int = 123 +exec @#v64_7890123456789$123456789$1234567890123456789012345678901234 = p8_atatuservar @@v63_7890123456789$123456789$123456789012345678901234567890123 output +select @#v64_7890123456789$123456789$1234567890123456789012345678901234, @@v63_7890123456789$123456789$123456789012345678901234567890123 +end +go +insert t6_trigger_atatuservar values (123) +go +~~START~~ +varchar +trigger tr6_atatuservar +~~END~~ + +~~START~~ +int +246 +~~END~~ + +~~START~~ +int#!#int +-246#!#246 +~~END~~ + +~~ROW COUNT: 1~~ + + +-- misc variable usage in expressions +declare @#v int = 3 +set @#v *= -1 +set @#v = @#v + 10*@#v +select @#v = @#v + 1 +if @#v > 0 select 'positive' else select 'negative' +select @#v, @#V, '@#v' +print @#v -- PRINT output not captured by JDBC tests +go +~~START~~ +varchar +negative +~~END~~ + +~~START~~ +int#!#int#!#varchar +-32#!#-32#!#@#v +~~END~~ + + +declare @@v int = 3 +set @@v *= -1 +set @@v = @@v + 10*@@v +select @@v = @@v + 1 +if @@v > 0 select 'positive' else select 'negative' +select @@v , @@V, '@@v' +print @@V -- PRINT output not captured by JDBC tests +go +~~START~~ +varchar +negative +~~END~~ + +~~START~~ +int#!#int#!#varchar +-32#!#-32#!#@@v +~~END~~ + + + +create procedure p10_atatuservar +as +declare @#v int = 3 +set @#v *= -1 +set @#v = @#v + 10*@#v +select @#v = @#v + 1 +if @#v > 0 select 'positive' else select 'negative' +select @#v, @#V, '@#v' +print @#v -- PRINT output not captured by JDBC tests +declare @@v int = 3 +set @@v *= -1 +set @@v = @@v + 10*@@v +select @@v = @@v + 1 +if @@v > 0 select 'positive' else select 'negative' +select @@v , @@V, '@@v' +print @@V -- PRINT output not captured by JDBC tests +go +exec p10_atatuservar +go +~~START~~ +varchar +negative +~~END~~ + +~~START~~ +int#!#int#!#varchar +-32#!#-32#!#@#v +~~END~~ + +~~START~~ +varchar +negative +~~END~~ + +~~START~~ +int#!#int#!#varchar +-32#!#-32#!#@@v +~~END~~ + + +declare @@c varchar(20)='abcd', @#v1 int=2, @v2@# int=1 +select substring(@@c, @#v1, @v2@#) +go +~~START~~ +varchar +b +~~END~~ + + +declare @@v1 varchar(20)='row 1', @#v2 varchar(20)='row 3' +select * from t1_atatuservar where c in (@@v1, @#v2) order by 1 +go +~~START~~ +varchar +row 1 +row 3 +~~END~~ + + +declare @@v1 varchar(20)='1', @#v2 varchar(20)='3' +select * from t1_atatuservar where c like '%'+@@v1 or c like '%'+@#v2 order by 1 +go +~~START~~ +varchar +row 1 +row 3 +~~END~~ + + + + +create procedure p11_atatuservar +as +declare @@c varchar(20)='abcd', @#v int=2, @v@# int=1 +select substring(@@c, @#v, @v@#) +declare @@v1 varchar(20)='row 1', @#v1 varchar(20)='row 3' +select * from t1_atatuservar where c in (@@v1, @#v1) order by 1 +declare @@v2 varchar(20)='1', @#v2 varchar(20)='3' +select * from t1_atatuservar where c like '%'+@@v2 or c like '%'+@#v2 order by 1 +go +exec p11_atatuservar +go +~~START~~ +varchar +b +~~END~~ + +~~START~~ +varchar +row 1 +row 3 +~~END~~ + +~~START~~ +varchar +row 1 +row 3 +~~END~~ + + +create function f4_atatuservar (@@p1 int, @#p2 int, @p5_atatuservar int) +returns table +as +return (select @@p1*@#p2*@p5_atatuservar as x where @@p1 = 123 or @#p2 = 10 or @p5_atatuservar = 1) +go +select * from dbo.f4_atatuservar(123, 20, 2) +go +~~START~~ +int +4920 +~~END~~ + +select * from dbo.f4_atatuservar(345, 10, 2) +go +~~START~~ +int +6900 +~~END~~ + +select * from dbo.f4_atatuservar(345, 20, 1) +go +~~START~~ +int +6900 +~~END~~ + + +declare @@c varchar(30), @#v int=1, @#v2 int=2 +set @@c = case when @#v > 0 then '> zero' else '<- zero' end +select @@c +set @@c = case @#v when 0 then 'zero' else '<> zero' end +select @@c +set @@c = case @#v when 0 then 'zero' when @#v2 then 'case 2' else 'something else' end +select @@c +set @@c = case @#v+@#v when 0 then 'zero' when @#v2 then 'case 2' else 'something else' end +select @@c +go +~~START~~ +varchar +> zero +~~END~~ + +~~START~~ +varchar +<> zero +~~END~~ + +~~START~~ +varchar +something else +~~END~~ + +~~START~~ +varchar +case 2 +~~END~~ + + +create procedure p22_atatuservar +as +declare @@c varchar(30), @#v int=1, @#v2 int=2 +set @@c = case when @#v > 0 then '> zero' else '<- zero' end +select @@c +set @@c = case @#v when 0 then 'zero' else '<> zero' end +select @@c +set @@c = case @#v when 0 then 'zero' when @#v2 then 'case 2' else 'something else' end +select @@c +set @@c = case @#v+@#v when 0 then 'zero' when @#v2 then 'case 2' else 'something else' end +select @@c +go +exec p22_atatuservar +go +~~START~~ +varchar +> zero +~~END~~ + +~~START~~ +varchar +<> zero +~~END~~ + +~~START~~ +varchar +something else +~~END~~ + +~~START~~ +varchar +case 2 +~~END~~ + + + + + + + +create trigger tr7_atatuservar on t7_trigger_atatuservar for insert +as +begin +select 'trigger tr7_atatuservar' +declare @@z128_@@@$$@@@@$$@@@@$$@@@@$$@@@@$$@@@@$$@@@@$$@@@@$$@@@@$$@@@_____############################################################ varchar(30), @#v64_7890123456789$123456789$1234567890123456789012345678901234 int=1, @#w64_7890123456789$123456789$1234567890123456789012345678901234 int=2 +set @@z128_@@@$$@@@@$$@@@@$$@@@@$$@@@@$$@@@@$$@@@@$$@@@@$$@@@@$$@@@_____############################################################ = case when @#v64_7890123456789$123456789$1234567890123456789012345678901234 > 0 then '> zero' else '<- zero' end +select @@z128_@@@$$@@@@$$@@@@$$@@@@$$@@@@$$@@@@$$@@@@$$@@@@$$@@@@$$@@@_____############################################################ +set @@z128_@@@$$@@@@$$@@@@$$@@@@$$@@@@$$@@@@$$@@@@$$@@@@$$@@@@$$@@@_____############################################################ = case @#v64_7890123456789$123456789$1234567890123456789012345678901234 when 0 then 'zero' else '<> zero' end +select @@z128_@@@$$@@@@$$@@@@$$@@@@$$@@@@$$@@@@$$@@@@$$@@@@$$@@@@$$@@@_____############################################################ +set @@z128_@@@$$@@@@$$@@@@$$@@@@$$@@@@$$@@@@$$@@@@$$@@@@$$@@@@$$@@@_____############################################################ = case @#v64_7890123456789$123456789$1234567890123456789012345678901234 when 0 then 'zero' when @#w64_7890123456789$123456789$1234567890123456789012345678901234 then 'case 2' else 'something else' end +select @@z128_@@@$$@@@@$$@@@@$$@@@@$$@@@@$$@@@@$$@@@@$$@@@@$$@@@@$$@@@_____############################################################ +set @@z128_@@@$$@@@@$$@@@@$$@@@@$$@@@@$$@@@@$$@@@@$$@@@@$$@@@@$$@@@_____############################################################ = case @#v64_7890123456789$123456789$1234567890123456789012345678901234+@#v64_7890123456789$123456789$1234567890123456789012345678901234 when 0 then 'zero' when @#w64_7890123456789$123456789$1234567890123456789012345678901234 then 'case 2' else 'something else' end +declare @#x64_7890123456789$123456789$1234567890123456789012345678901234 int = 3 +set @#x64_7890123456789$123456789$1234567890123456789012345678901234 *= -1 +set @#x64_7890123456789$123456789$1234567890123456789012345678901234 = @#x64_7890123456789$123456789$1234567890123456789012345678901234 + 10*@#x64_7890123456789$123456789$1234567890123456789012345678901234 +select @#x64_7890123456789$123456789$1234567890123456789012345678901234 = @#x64_7890123456789$123456789$1234567890123456789012345678901234 + 1 +if @#x64_7890123456789$123456789$1234567890123456789012345678901234 > 0 select 'positive' else select 'negative' +select @#x64_7890123456789$123456789$1234567890123456789012345678901234, @#x64_7890123456789$123456789$1234567890123456789012345678901234, '@#x64_7890123456789$123456789$1234567890123456789012345678901234' +print @#x64_7890123456789$123456789$1234567890123456789012345678901234 -- PRINT output not captured by JDBC tests +declare @@v128_@@@$$@@@@$$@########@@@$$@@@@$$@@@@$$@@@###__##@@@$$@@@@$$@@@@$$@@@@$$@@@______@@@$$@@@@$$@@@@$$@@@###########@@@$$@@@@@@ int = 3 +set @@v128_@@@$$@@@@$$@########@@@$$@@@@$$@@@@$$@@@###__##@@@$$@@@@$$@@@@$$@@@@$$@@@______@@@$$@@@@$$@@@@$$@@@###########@@@$$@@@@@@ *= -1 +set @@v128_@@@$$@@@@$$@########@@@$$@@@@$$@@@@$$@@@###__##@@@$$@@@@$$@@@@$$@@@@$$@@@______@@@$$@@@@$$@@@@$$@@@###########@@@$$@@@@@@ = @@v128_@@@$$@@@@$$@########@@@$$@@@@$$@@@@$$@@@###__##@@@$$@@@@$$@@@@$$@@@@$$@@@______@@@$$@@@@$$@@@@$$@@@###########@@@$$@@@@@@ + 10*@@v128_@@@$$@@@@$$@########@@@$$@@@@$$@@@@$$@@@###__##@@@$$@@@@$$@@@@$$@@@@$$@@@______@@@$$@@@@$$@@@@$$@@@###########@@@$$@@@@@@ +select @@v128_@@@$$@@@@$$@########@@@$$@@@@$$@@@@$$@@@###__##@@@$$@@@@$$@@@@$$@@@@$$@@@______@@@$$@@@@$$@@@@$$@@@###########@@@$$@@@@@@ = @@v128_@@@$$@@@@$$@########@@@$$@@@@$$@@@@$$@@@###__##@@@$$@@@@$$@@@@$$@@@@$$@@@______@@@$$@@@@$$@@@@$$@@@###########@@@$$@@@@@@ + 1 +if @@v128_@@@$$@@@@$$@########@@@$$@@@@$$@@@@$$@@@###__##@@@$$@@@@$$@@@@$$@@@@$$@@@______@@@$$@@@@$$@@@@$$@@@###########@@@$$@@@@@@ > 0 select 'positive' else select 'negative' +select @@v128_@@@$$@@@@$$@########@@@$$@@@@$$@@@@$$@@@###__##@@@$$@@@@$$@@@@$$@@@@$$@@@______@@@$$@@@@$$@@@@$$@@@###########@@@$$@@@@@@ , @@v128_@@@$$@@@@$$@########@@@$$@@@@$$@@@@$$@@@###__##@@@$$@@@@$$@@@@$$@@@@$$@@@______@@@$$@@@@$$@@@@$$@@@###########@@@$$@@@@@@, '@@v128_@@@$$@@@@$$@########@@@$$@@@@$$@@@@$$@@@###__##@@@$$@@@@$$@@@@$$@@@@$$@@@______@@@$$@@@@$$@@@@$$@@@###########@@@$$@@@@@@' +print @@v128_@@@$$@@@@$$@########@@@$$@@@@$$@@@@$$@@@###__##@@@$$@@@@$$@@@@$$@@@@$$@@@______@@@$$@@@@$$@@@@$$@@@###########@@@$$@@@@@@ -- PRINT output not captured by JDBC tests +declare @@y128_############################################______@@@$$@@@@$$@@@@__###############@@@$$@@@@$$@@@@$$@@@@$$@@@@$$@@@@$$@@@@ varchar(20)='abcd', @#p64_7890123456789$123456789$1234567890123456789012345678901234 int=2, @@#q64_890123456789$123456789$1234567890123456789012345678901234 int=1 +select substring(@@y128_############################################______@@@$$@@@@$$@@@@__###############@@@$$@@@@$$@@@@$$@@@@$$@@@@$$@@@@$$@@@@, @#p64_7890123456789$123456789$1234567890123456789012345678901234, @@#q64_890123456789$123456789$1234567890123456789012345678901234) +declare @@q64_7890123456789$123456789$1234567890123456789012345678901234 varchar(20)='1', @#q4_7890123456789$123456789$1234567890123456789012345678901234 varchar(20)='3' +select * from t1_atatuservar where c like '%'+@@q64_7890123456789$123456789$1234567890123456789012345678901234 or c like '%'+@#q4_7890123456789$123456789$1234567890123456789012345678901234 order by 1 +end +go +insert t7_trigger_atatuservar values (123) +go +~~START~~ +varchar +trigger tr7_atatuservar +~~END~~ + +~~START~~ +varchar +> zero +~~END~~ + +~~START~~ +varchar +<> zero +~~END~~ + +~~START~~ +varchar +something else +~~END~~ + +~~START~~ +varchar +negative +~~END~~ + +~~START~~ +int#!#int#!#varchar +-32#!#-32#!#@#x64_7890123456789$123456789$1234567890123456789012345678901234 +~~END~~ + +~~START~~ +varchar +negative +~~END~~ + +~~START~~ +int#!#int#!#varchar +-32#!#-32#!#@@v128_@@@$$@@@@$$@########@@@$$@@@@$$@@@@$$@@@###__##@@@$$@@@@$$@@@@$$@@@@$$@@@______@@@$$@@@@$$@@@@$$@@@###########@@@$$@@@@@@ +~~END~~ + +~~START~~ +varchar +b +~~END~~ + +~~START~~ +varchar +row 1 +row 3 +~~END~~ + +~~ROW COUNT: 1~~ + + +-- table variables, in T-SQL batch, procedure and trigger (functions are below) +declare @@v table (a int) +insert @@v values (123) +select * from @@v +go +~~ROW COUNT: 1~~ + +~~START~~ +int +123 +~~END~~ + + +declare @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 table (a int) +insert @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 values (123) +select * from @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 +go +~~ROW COUNT: 1~~ + +~~START~~ +int +123 +~~END~~ + + +declare @#v table (a int) +insert @#v values (123) +insert @#v select * from @#v +select * from @#v +update @#v set a = a + 1 +select * from @#v +set rowcount 1 +delete @#v +set rowcount 0 +select * from @#v +go +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~START~~ +int +123 +123 +~~END~~ + +~~ROW COUNT: 2~~ + +~~START~~ +int +124 +124 +~~END~~ + +~~ROW COUNT: 1~~ + +~~START~~ +int +124 +~~END~~ + + +declare @@V table (a int) +insert @@v values (123) +insert @@v select * from @@v +select * from @@v +update @@v set a = a + 1 +select * from @@v +set rowcount 1 +delete @@v +set rowcount 0 +select * from @@v +go +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~START~~ +int +123 +123 +~~END~~ + +~~ROW COUNT: 2~~ + +~~START~~ +int +124 +124 +~~END~~ + +~~ROW COUNT: 1~~ + +~~START~~ +int +124 +~~END~~ + + + +create procedure p12_atatuservar +as +declare @#v table (a int) +insert @#v values (123) +insert @#v select * from @#v +select * from @#v +update @#v set a = a + 1 +select * from @#v +set rowcount 1 +delete @#v +set rowcount 0 +select * from @#v +declare @@V table (a int) +insert @@v values (123) +insert @@v select * from @@v +select * from @@v +update @@v set a = a + 1 +select * from @@v +set rowcount 1 +delete @@v +set rowcount 0 +select * from @@v +go +exec p12_atatuservar +go +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~START~~ +int +123 +123 +~~END~~ + +~~ROW COUNT: 2~~ + +~~START~~ +int +124 +124 +~~END~~ + +~~ROW COUNT: 1~~ + +~~START~~ +int +124 +~~END~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~START~~ +int +123 +123 +~~END~~ + +~~ROW COUNT: 2~~ + +~~START~~ +int +124 +124 +~~END~~ + +~~ROW COUNT: 1~~ + +~~START~~ +int +124 +~~END~~ + + +create procedure p13_atatuservar +as +declare @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 table (a int) +insert @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 values (123) +select * from @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 +insert @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 select * from @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 +select * from @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 +update @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 set a = a + 1 +select * from @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 +set rowcount 1 +delete @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 +set rowcount 0 +select * from @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 +go +exec p13_atatuservar +go +~~ROW COUNT: 1~~ + +~~START~~ +int +123 +~~END~~ + +~~ROW COUNT: 1~~ + +~~START~~ +int +123 +123 +~~END~~ + +~~ROW COUNT: 2~~ + +~~START~~ +int +124 +124 +~~END~~ + +~~ROW COUNT: 1~~ + +~~START~~ +int +124 +~~END~~ + + + + +create trigger tr8_atatuservar on t8_trigger_atatuservar for insert +as +begin +select 'trigger tr8_atatuservar' +declare @#v table (a int) +insert @#v values (123) +insert @#v select * from @#v +select * from @#v +update @#v set a = a + 1 +select * from @#v +set rowcount 1 +delete @#v +set rowcount 0 +select * from @#v +declare @@V table (a int) +insert @@v values (123) +insert @@v select * from @@v +select * from @@v +update @@v set a = a + 1 +select * from @@v +set rowcount 1 +delete @@v +set rowcount 0 +select * from @@v +end +go +insert t8_trigger_atatuservar values (123) +go +~~START~~ +varchar +trigger tr8_atatuservar +~~END~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~START~~ +int +123 +123 +~~END~~ + +~~ROW COUNT: 2~~ + +~~START~~ +int +124 +124 +~~END~~ + +~~ROW COUNT: 1~~ + +~~START~~ +int +124 +~~END~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~START~~ +int +123 +123 +~~END~~ + +~~ROW COUNT: 2~~ + +~~START~~ +int +124 +124 +~~END~~ + +~~ROW COUNT: 1~~ + +~~START~~ +int +124 +~~END~~ + +~~ROW COUNT: 1~~ + + + +create trigger tr9_atatuservar on t9_trigger_atatuservar for insert +as +begin +select 'trigger tr9_atatuservar' +declare @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 table (a int) +insert @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 values (123) +select * from @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 +insert @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 select * from @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 +select * from @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 +update @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 set a = a + 1 +select * from @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 +set rowcount 1 +delete @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 +set rowcount 0 +select * from @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 +end +go +insert t9_trigger_atatuservar values (123) +go +~~START~~ +varchar +trigger tr9_atatuservar +~~END~~ + +~~ROW COUNT: 1~~ + +~~START~~ +int +123 +~~END~~ + +~~ROW COUNT: 1~~ + +~~START~~ +int +123 +123 +~~END~~ + +~~ROW COUNT: 2~~ + +~~START~~ +int +124 +124 +~~END~~ + +~~ROW COUNT: 1~~ + +~~START~~ +int +124 +~~END~~ + +~~ROW COUNT: 1~~ + + +-- @@rowcount +declare @@V int = 1 +set rowcount @@v +select * from t1_atatuservar order by 1 +set @@v = 0 +set rowcount @@v +select * from t1_atatuservar order by 1 +go +~~START~~ +varchar +row 1 +~~END~~ + +~~START~~ +varchar +row 1 +row 2 +row 3 +~~END~~ + + +declare @#V int = 1 +set rowcount @#v +select * from t1_atatuservar order by 1 +set @#v = 0 +set rowcount @#v +select * from t1_atatuservar order by 1 +go +~~START~~ +varchar +row 1 +~~END~~ + +~~START~~ +varchar +row 1 +row 2 +row 3 +~~END~~ + + + +create procedure p14_atatuservar +as +declare @@V int = 1 +set rowcount @@v +select * from t1_atatuservar order by 1 +set @@v = 0 +set rowcount @@v +select * from t1_atatuservar order by 1 +declare @#V int = 1 +set rowcount @#v +select * from t1_atatuservar order by 1 +set @#v = 0 +set rowcount @#v +select * from t1_atatuservar order by 1 +go +exec p14_atatuservar +go +~~START~~ +varchar +row 1 +~~END~~ + +~~START~~ +varchar +row 1 +row 2 +row 3 +~~END~~ + +~~START~~ +varchar +row 1 +~~END~~ + +~~START~~ +varchar +row 1 +row 2 +row 3 +~~END~~ + + + + +create trigger tr10_atatuservar on t10_trigger_atatuservar for insert +as +begin +select 'trigger tr10_atatuservar' +declare @@V int = 1 +set rowcount @@v +select * from t1_atatuservar order by 1 +set @@v = 0 +set rowcount @@v +select * from t1_atatuservar order by 1 +declare @#V int = 1 +set rowcount @#v +select * from t1_atatuservar order by 1 +set @#v = 0 +set rowcount @#v +select * from t1_atatuservar order by 1 +end +go +insert t10_trigger_atatuservar values (123) +go +~~START~~ +varchar +trigger tr10_atatuservar +~~END~~ + +~~START~~ +varchar +row 1 +~~END~~ + +~~START~~ +varchar +row 1 +row 2 +row 3 +~~END~~ + +~~START~~ +varchar +row 1 +~~END~~ + +~~START~~ +varchar +row 1 +row 2 +row 3 +~~END~~ + +~~ROW COUNT: 1~~ + + +create function f1_atatuservar_extra_long_name_here() +returns @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 table (a int) +as +begin +insert @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 +values (123) +return +end +go +select * from f1_atatuservar_extra_long_name_here() +go +~~START~~ +int +123 +~~END~~ + + +create function f2_atatuservar_extra_long_name_here() +returns @#v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 table (a int) +as +begin +insert @#v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 +values (123) +return +end +go +select * from f2_atatuservar_extra_long_name_here() +go +~~START~~ +int +123 +~~END~~ + + +-- in SET statements that support variable argument, procedure and trigger (functions are below) +declare @@v int = 4 +set datefirst @@v +select datepart(dw, '2024-10-21') +go +~~START~~ +int +5 +~~END~~ + +set datefirst 7 +go + +declare @#v int = 5 +set datefirst @#v +select datepart(dw, '2024-10-21') +go +~~START~~ +int +4 +~~END~~ + +set datefirst 7 +go + +declare @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 int = 5 +set datefirst @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 +select datepart(dw, '2024-10-21') +go +~~START~~ +int +4 +~~END~~ + +set datefirst 7 +go + +-- SET DATEFORMAT currently ignores the argument and will always use 'mdy' +declare @@v varchar(10) = 'dmy' +set dateformat @@v +select cast('01/02/03' as datetime) +go +~~START~~ +datetime +2003-01-02 00:00:00.0 +~~END~~ + +set dateformat mdy +go + +-- SET DATEFORMAT currently silently ignores the argument and will always use 'mdy' +declare @#v varchar(10) = 'dmy' +set dateformat @#v +select cast('01/02/03' as datetime) +go +~~START~~ +datetime +2003-01-02 00:00:00.0 +~~END~~ + +set dateformat mdy +go + +-- SET DATEFORMAT currently silently ignores the argument and will always use 'mdy' +declare @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 varchar(10) = 'dmy' +set dateformat @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 +select cast('01/02/03' as datetime) +go +~~START~~ +datetime +2003-01-02 00:00:00.0 +~~END~~ + +set dateformat mdy +go + +-- SET LANGUAGE currently only supports english +declare @@v varchar(10) = 'dutch' +set language @@v +select @@language +go +~~WARNING (Code: 5703)~~ + +~~WARNING (Message: Changed language setting to 'us_english' Server SQLState: S0001)~~ + +~~START~~ +nvarchar +us_english +~~END~~ + +set language us_english +go +declare @#v varchar(10) = 'french' +set language @#v +select @@language +go +~~WARNING (Code: 5703)~~ + +~~WARNING (Message: Changed language setting to 'us_english' Server SQLState: S0001)~~ + +~~START~~ +nvarchar +us_english +~~END~~ + +set language us_english +go +declare @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 varchar(10) = 'french' +set language @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 +select @@language +go +~~WARNING (Code: 5703)~~ + +~~WARNING (Message: Changed language setting to 'us_english' Server SQLState: S0001)~~ + +~~START~~ +nvarchar +us_english +~~END~~ + +set language us_english +go + + + + +create procedure p15_atatuservar +as +declare @@v1 int = 4 +set datefirst @@v1 +select datepart(dw, '2024-10-21') +declare @#v1 int = 5 +set datefirst @#v1 +select datepart(dw, '2024-10-21') +declare @@v2 varchar(10) = 'dmy' +set dateformat @@v2 +select cast('01/02/03' as datetime) +declare @#v3 varchar(10) = 'dutch' +set language @#v3 +select @@language +go + +exec p15_atatuservar +go +~~START~~ +int +5 +~~END~~ + +~~START~~ +int +4 +~~END~~ + +~~START~~ +datetime +2003-01-02 00:00:00.0 +~~END~~ + +~~START~~ +nvarchar +us_english +~~END~~ + +set datefirst 7 +set dateformat mdy +set language us_english +go + + + +create procedure p16_atatuservar +as +declare @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 int = 4 +set datefirst @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 +select datepart(dw, '2024-10-21') +declare @#v128_90123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 int = 5 +set datefirst @#v128_90123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 +select datepart(dw, '2024-10-21') +declare @@w128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 varchar(10) = 'dmy' +set dateformat @@w128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 +select cast('01/02/03' as datetime) +declare @#w128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 varchar(10) = 'dutch' +set language @#w128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 +select @@language +go +exec p15_atatuservar +go +~~START~~ +int +5 +~~END~~ + +~~START~~ +int +4 +~~END~~ + +~~START~~ +datetime +2003-01-02 00:00:00.0 +~~END~~ + +~~START~~ +nvarchar +us_english +~~END~~ + +set datefirst 7 +set dateformat mdy +set language us_english +go + + + + +create trigger tr11_atatuservar on t11_trigger_atatuservar for insert +as +begin +select 'trigger tr11_atatuservar' + +declare @@v1 int = 4 +set datefirst @@v1 +select datepart(dw, '2024-10-21') +declare @#v1 int = 5 +set datefirst @#v1 +select datepart(dw, '2024-10-21') +declare @@v2 varchar(10) = 'dmy' +set dateformat @@v2 +select cast('01/02/03' as datetime) +declare @#v3 varchar(10) = 'dutch' +set language @#v3 +select @@language +end +go +insert t11_trigger_atatuservar values (123) +go +~~START~~ +varchar +trigger tr11_atatuservar +~~END~~ + +~~START~~ +int +5 +~~END~~ + +~~START~~ +int +4 +~~END~~ + +~~START~~ +datetime +2003-01-02 00:00:00.0 +~~END~~ + +~~START~~ +nvarchar +us_english +~~END~~ + +~~ROW COUNT: 1~~ + +set datefirst 7 +set dateformat mdy +set language us_english +go + + + + +create trigger tr12_atatuservar on t12_trigger_atatuservar for insert +as +begin +select 'trigger tr12_atatuservar' + +declare @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 int = 4 +set datefirst @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 +select datepart(dw, '2024-10-21') +declare @#v128_90123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 int = 5 +set datefirst @#v128_90123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 +select datepart(dw, '2024-10-21') +declare @@w128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 varchar(10) = 'dmy' +set dateformat @@w128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 +select cast('01/02/03' as datetime) +declare @#w128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 varchar(10) = 'dutch' +set language @#w128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 +select @@language +end +go +insert t12_trigger_atatuservar values (123) +go +~~START~~ +varchar +trigger tr12_atatuservar +~~END~~ + +~~START~~ +int +5 +~~END~~ + +~~START~~ +int +4 +~~END~~ + +~~START~~ +datetime +2003-01-02 00:00:00.0 +~~END~~ + +~~START~~ +nvarchar +us_english +~~END~~ + +~~ROW COUNT: 1~~ + +set datefirst 7 +set dateformat mdy +set language us_english +go + +-- execute-immediate, procedure and trigger +declare @@V varchar(50) = 'select 123' execute(@@v) +go +~~START~~ +int +123 +~~END~~ + + +declare @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 varchar(50) = 'select 123' execute(@@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678) +go +~~START~~ +int +123 +~~END~~ + + +declare @@V varchar(50) = 'declare @#v int =123 select @#v' +execute(@@v) +go +~~START~~ +int +123 +~~END~~ + + +declare @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 varchar(500) = 'declare @#w128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 int =123 select @#w128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678' +execute(@@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678) +go +~~START~~ +int +123 +~~END~~ + + +declare @@V varchar(50) = 'declare @#v int =123 select @#v' +execute('select 456 ' + @@v) +go +~~START~~ +int +456 +~~END~~ + +~~START~~ +int +123 +~~END~~ + + +declare @#V varchar(50) = 'declare @@v int =123 select @@v' +execute(@#v) +go +~~START~~ +int +123 +~~END~~ + + +declare @#V varchar(50) = 'declare @@v int =123 select @@v' +execute('select 456 ' + @#v) +go +~~START~~ +int +456 +~~END~~ + +~~START~~ +int +123 +~~END~~ + + +declare @#V varchar(50) = 'select 123 ' +execute(@#v + @#v) +go +~~START~~ +int +123 +~~END~~ + +~~START~~ +int +123 +~~END~~ + + +declare @#V varchar(50) = 'select 123 ', @@V varchar(50) = 'select 456 ' +execute(@#v + @@v + @@v + @#v) +go +~~START~~ +int +123 +~~END~~ + +~~START~~ +int +456 +~~END~~ + +~~START~~ +int +456 +~~END~~ + +~~START~~ +int +123 +~~END~~ + + +declare @@V varchar(50) = 'select "abc''def" ' +execute(@@v + 'select "xy''z" ' + @@V) +go +~~START~~ +varchar +abc'def +~~END~~ + +~~START~~ +varchar +xy'z +~~END~~ + +~~START~~ +varchar +abc'def +~~END~~ + + + + + +create procedure p17_atatuservar +as +declare @#V1 varchar(50) = 'declare @@v int =123 select @@v' +execute('select 456 ' + @#v1) +declare @#V2 varchar(50) = 'select 123 ' +execute(@#v2 + @#v2) +declare @#V3 varchar(50) = 'select 123 ', @@V varchar(50) = 'select 456 ' +execute(@#v3 + @@v + @@v + @#v3) +declare @@V4 varchar(50) = 'select "abc''def" ' +execute(@@v4 + 'select "xy''z" ' + @@V4) +go +exec p17_atatuservar +go +~~START~~ +int +456 +~~END~~ + +~~START~~ +int +123 +~~END~~ + +~~START~~ +int +123 +~~END~~ + +~~START~~ +int +123 +~~END~~ + +~~START~~ +int +123 +~~END~~ + +~~START~~ +int +456 +~~END~~ + +~~START~~ +int +456 +~~END~~ + +~~START~~ +int +123 +~~END~~ + +~~START~~ +varchar +abc'def +~~END~~ + +~~START~~ +varchar +xy'z +~~END~~ + +~~START~~ +varchar +abc'def +~~END~~ + + + + + + +create trigger tr13_atatuservar on t13_trigger_atatuservar for insert +as +begin +select 'trigger tr13_atatuservar' +declare @#V1 varchar(50) = 'declare @@v int =123 select @@v' +execute('select 456 ' + @#v1) +declare @#V2 varchar(50) = 'select 123 ' +execute(@#v2 + @#v2) +declare @#V3 varchar(50) = 'select 123 ', @@V varchar(50) = 'select 456 ' +execute(@#v3 + @@v + @@v + @#v3) +declare @@V4 varchar(50) = 'select "abc''def" ' +execute(@@v4 + 'select "xy''z" ' + @@V4) +end +go +insert t13_trigger_atatuservar values (123) +go +~~START~~ +varchar +trigger tr13_atatuservar +~~END~~ + +~~START~~ +int +456 +~~END~~ + +~~START~~ +int +123 +~~END~~ + +~~START~~ +int +123 +~~END~~ + +~~START~~ +int +123 +~~END~~ + +~~START~~ +int +123 +~~END~~ + +~~START~~ +int +456 +~~END~~ + +~~START~~ +int +456 +~~END~~ + +~~START~~ +int +123 +~~END~~ + +~~START~~ +varchar +abc'def +~~END~~ + +~~START~~ +varchar +xy'z +~~END~~ + +~~START~~ +varchar +abc'def +~~END~~ + +~~ROW COUNT: 1~~ + + +create procedure p18_atatuservar +as +begin +declare @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 varchar(500) = 'declare @#w128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 int =123 select @#w128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678' +execute(@@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678) +end +go +exec p18_atatuservar +go +~~START~~ +int +123 +~~END~~ + + + +create trigger tr14_atatuservar on t14_trigger_atatuservar for insert +as +begin +select 'trigger tr14_atatuservar' +declare @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 varchar(500) = 'declare @#w128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 int =123 select @#w128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678' +execute(@@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678) +end +go +insert t14_trigger_atatuservar values (123) +go +~~START~~ +varchar +trigger tr14_atatuservar +~~END~~ + +~~START~~ +int +123 +~~END~~ + +~~ROW COUNT: 1~~ + + +-- RAISERROR, procedure and trigger +declare @@v int=50001 +raiserror(@@v,1,1) +go + +declare @@v int=50001, @@v2 int=1 +raiserror(@@v,@@v2,1) +go + +declare @@v int=50001, @@v2 int=1, @#v3 int=1 +raiserror(@@v,@@v2,@#v3) +go + +declare @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 int=50001, @#w128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 int=1, @@y128_############################################______@@@$$@@@@$$@@@@__###############@@@$$@@@@$$@@@@$$@@@@$$@@@@$$@@@@$$@@@@ int=1 +raiserror(@@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678, @#w128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678, @@y128_############################################______@@@$$@@@@$$@@@@__###############@@@$$@@@@$$@@@@$$@@@@$$@@@@$$@@@@$$@@@@) +go + +create procedure p1_raiserror_atatuservar +as +declare @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 int=50001, @#w128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 int=1, @@y128_############################################______@@@$$@@@@$$@@@@__###############@@@$$@@@@$$@@@@$$@@@@$$@@@@$$@@@@$$@@@@ int=1 +raiserror(@@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678, @#w128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678, @@y128_############################################______@@@$$@@@@$$@@@@__###############@@@$$@@@@$$@@@@$$@@@@$$@@@@$$@@@@$$@@@@) +go +exec p1_raiserror_atatuservar +go + +create trigger tr15_atatuservar on t15_trigger_atatuservar for insert +as +begin +declare @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 int=50001, @#w128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 int=1, @@y128_############################################______@@@$$@@@@$$@@@@__###############@@@$$@@@@$$@@@@$$@@@@$$@@@@$$@@@@$$@@@@ int=1 +raiserror(@@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678, @#w128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678, @@y128_############################################______@@@$$@@@@$$@@@@__###############@@@$$@@@@$$@@@@$$@@@@$$@@@@$$@@@@$$@@@@) +end +go +insert t15_trigger_atatuservar values (123) +go +~~WARNING (Code: 0)~~ + +~~WARNING (Message: No. 50001 in sys.messages Server SQLState: S0001)~~ + +~~ROW COUNT: 1~~ + + + +-- sp_executesql with named/unnamed arguments, output parameters, in T-SQL batch, procedure, trigger +exec sp_executesql N'declare @@v int=123 select @@v' +go +~~START~~ +int +123 +~~END~~ + + +exec sp_executesql N'SELECT @@v1, @#v2', N'@@v1 int, @#v2 varchar(20)', @#v2='Hello, World!', @@v1=123 +go +~~START~~ +int#!#varchar +123#!#Hello, World! +~~END~~ + + +declare @@sql nvarchar(50)=N'SELECT @@v1, @#v2' +exec sp_executesql @@sql, N'@@v1 int, @#v2 varchar(20)', @#v2='Hello, World!', @@v1=123 +go +~~START~~ +int#!#varchar +123#!#Hello, World! +~~END~~ + + +declare @@params nvarchar(50)=N'@@v1 int, @#v2 varchar(20)' +exec sp_executesql N'SELECT @@v1, @#v2', @@params, @#v2='Hello, World!', @@v1=123 +go +~~START~~ +int#!#varchar +123#!#Hello, World! +~~END~~ + + +declare @@sql nvarchar(50)=N'SELECT @@v1, @#v2' +declare @@params nvarchar(50)=N'@@v1 int, @#v2 varchar(20)' +exec sp_executesql @@sql, @@params, @#v2='Hello, World!', @@v1=123 +go +~~START~~ +int#!#varchar +123#!#Hello, World! +~~END~~ + + +declare @@sql nvarchar(50)=N'SELECT @@v1, @#v2', @@params nvarchar(50)=N'@@v1 int, @#v2 varchar(20)', @@v3 int=123, @#v4 varchar(20)='Hello, World!' +exec sp_executesql @@sql, @@params, @#v2=@#v4, @@v1=@@v3 +go +~~START~~ +int#!#varchar +123#!#Hello, World! +~~END~~ + + +declare @@SQLString1 NVARCHAR(100); +declare @#SQLString2 NVARCHAR(max); +declare @@ParamDef NVARCHAR(100); +SET @@SQLString1 = N'SET @#a = @@b + @@b'; +SET @#SQLString2 = N'EXEC sp_executesql N''SET @#a = @@b + @@b'', N''@#a INT OUT, @@b INT'', @#a OUTPUT, @@b;'; +SET @@ParamDef = N'@#a INT OUTPUT, @@b INT'; +declare @@p INT; +declare @#a INT; +EXEC sp_executesql @@SQLString1, @@ParamDef, @#a = @@p OUT, @@b = 10; +EXEC sp_executesql @#SQLString2, @@ParamDef, @#a = @#a OUT, @@b = 11; +SELECT @@p, @#a; +go +~~START~~ +int#!#int +20#!#22 +~~END~~ + + +create procedure p1_spexec_atatuservar +as +declare @@sql nvarchar(50)=N'SELECT @@v1, @#v2', @@params nvarchar(50)=N'@@v1 int, @#v2 varchar(20)', @@v3 int=123, @#v4 varchar(20)='Hello, World!' +exec sp_executesql @@sql, @@params, @#v2=@#v4, @@v1=@@v3 +declare @@SQLString1 NVARCHAR(100) +declare @#SQLString2 NVARCHAR(max) +declare @@ParamDef NVARCHAR(100) +SET @@SQLString1 = N'SET @#a = @@b + @@b' +SET @#SQLString2 = N'EXEC sp_executesql N''SET @#a = @@b + @@b'', N''@#a INT OUT, @@b INT'', @#a OUT, @@b' +SET @@ParamDef = N'@#a INT OUTPUT, @@b INT' +declare @@p INT +declare @#a INT +EXEC sp_executesql @@SQLString1, @@ParamDef, @#a = @@p OUT, @@b = 10 +EXEC sp_executesql @#SQLString2, @@ParamDef, @#a = @#a OUT, @@b = 11 +SELECT @@p, @#a +go +exec p1_spexec_atatuservar +go +~~START~~ +int#!#varchar +123#!#Hello, World! +~~END~~ + +~~START~~ +int#!#int +20#!#22 +~~END~~ + + + +create trigger tr16_atatuservar on t16_trigger_atatuservar for insert +as +begin +select 'trigger tr16_atatuservar' +declare @@sql nvarchar(50)=N'SELECT @@v1, @#v2', @@params nvarchar(50)=N'@@v1 int, @#v2 varchar(20)', @@v3 int=123, @#v4 varchar(20)='Hello, World!' +exec sp_executesql @@sql, @@params, @#v2=@#v4, @@v1=@@v3 +declare @@SQLString1 NVARCHAR(100) +declare @#SQLString2 NVARCHAR(max) +declare @@ParamDef NVARCHAR(100) +SET @@SQLString1 = N'SET @#a = @@b + @@b' +SET @#SQLString2 = N'EXEC sp_executesql N''SET @#a = @@b + @@b'', N''@#a INT OUT, @@b INT'', @#a OUT, @@b' +SET @@ParamDef = N'@#a INT OUTPUT, @@b INT' +declare @@p INT +declare @#a INT +EXEC sp_executesql @@SQLString1, @@ParamDef, @#a = @@p OUT, @@b = 10 +EXEC sp_executesql @#SQLString2, @@ParamDef, @#a = @#a OUT, @@b = 11 +SELECT @@p, @#a +end +go +insert t16_trigger_atatuservar values (123) +go +~~START~~ +varchar +trigger tr16_atatuservar +~~END~~ + +~~START~~ +int#!#varchar +123#!#Hello, World! +~~END~~ + +~~START~~ +int#!#int +20#!#22 +~~END~~ + +~~ROW COUNT: 1~~ + + +declare @@v63_7890123456789$123456789$123456789012345678901234567890123 NVARCHAR(1000); +declare @#w63_7890123456789$123456789$123456789012345678901234567890123 NVARCHAR(max); +declare @@x63_7890123456789$123456789$123456789012345678901234567890123 NVARCHAR(1000); +SET @@v63_7890123456789$123456789$123456789012345678901234567890123 = N'SET @@z63_7890123456789$123456789$123456789012345678901234567890123 = @@p63_7890123456789$123456789$123456789012345678901234567890123 + @@p63_7890123456789$123456789$123456789012345678901234567890123'; +SET @#w63_7890123456789$123456789$123456789012345678901234567890123 = N'EXEC sp_executesql N''SET @@z63_7890123456789$123456789$123456789012345678901234567890123 = @@p63_7890123456789$123456789$123456789012345678901234567890123 + @@p63_7890123456789$123456789$123456789012345678901234567890123'', N''@@z63_7890123456789$123456789$123456789012345678901234567890123 INT OUT, @@p63_7890123456789$123456789$123456789012345678901234567890123 INT'', @@z63_7890123456789$123456789$123456789012345678901234567890123 OUTPUT, @@p63_7890123456789$123456789$123456789012345678901234567890123;'; +SET @@x63_7890123456789$123456789$123456789012345678901234567890123 = N'@@z63_7890123456789$123456789$123456789012345678901234567890123 INT OUTPUT, @@p63_7890123456789$123456789$123456789012345678901234567890123 INT'; +declare @#y63_7890123456789$123456789$123456789012345678901234567890123 INT; +declare @@z63_7890123456789$123456789$123456789012345678901234567890123 INT; +EXEC sp_executesql @@v63_7890123456789$123456789$123456789012345678901234567890123, @@x63_7890123456789$123456789$123456789012345678901234567890123, @@z63_7890123456789$123456789$123456789012345678901234567890123 = @#y63_7890123456789$123456789$123456789012345678901234567890123 OUT, @@p63_7890123456789$123456789$123456789012345678901234567890123 = 10; +EXEC sp_executesql @#w63_7890123456789$123456789$123456789012345678901234567890123, @@x63_7890123456789$123456789$123456789012345678901234567890123, @@z63_7890123456789$123456789$123456789012345678901234567890123 = @@z63_7890123456789$123456789$123456789012345678901234567890123 OUT, @@p63_7890123456789$123456789$123456789012345678901234567890123 = 11; +SELECT @#y63_7890123456789$123456789$123456789012345678901234567890123, @@z63_7890123456789$123456789$123456789012345678901234567890123; +go +~~START~~ +int#!#int +20#!#22 +~~END~~ + + +create procedure p2_spexec_atatuservar +as +declare @@v63_7890123456789$123456789$123456789012345678901234567890123 NVARCHAR(1000); +declare @#w63_7890123456789$123456789$123456789012345678901234567890123 NVARCHAR(max); +declare @@x63_7890123456789$123456789$123456789012345678901234567890123 NVARCHAR(1000); +SET @@v63_7890123456789$123456789$123456789012345678901234567890123 = N'SET @@z63_7890123456789$123456789$123456789012345678901234567890123 = @@p63_7890123456789$123456789$123456789012345678901234567890123 + @@p63_7890123456789$123456789$123456789012345678901234567890123'; +SET @#w63_7890123456789$123456789$123456789012345678901234567890123 = N'EXEC sp_executesql N''SET @@z63_7890123456789$123456789$123456789012345678901234567890123 = @@p63_7890123456789$123456789$123456789012345678901234567890123 + @@p63_7890123456789$123456789$123456789012345678901234567890123'', N''@@z63_7890123456789$123456789$123456789012345678901234567890123 INT OUT, @@p63_7890123456789$123456789$123456789012345678901234567890123 INT'', @@z63_7890123456789$123456789$123456789012345678901234567890123 OUTPUT, @@p63_7890123456789$123456789$123456789012345678901234567890123;'; +SET @@x63_7890123456789$123456789$123456789012345678901234567890123 = N'@@z63_7890123456789$123456789$123456789012345678901234567890123 INT OUTPUT, @@p63_7890123456789$123456789$123456789012345678901234567890123 INT'; +declare @#y63_7890123456789$123456789$123456789012345678901234567890123 INT; +declare @@z63_7890123456789$123456789$123456789012345678901234567890123 INT; +EXEC sp_executesql @@v63_7890123456789$123456789$123456789012345678901234567890123, @@x63_7890123456789$123456789$123456789012345678901234567890123, @@z63_7890123456789$123456789$123456789012345678901234567890123 = @#y63_7890123456789$123456789$123456789012345678901234567890123 OUT, @@p63_7890123456789$123456789$123456789012345678901234567890123 = 10; +EXEC sp_executesql @#w63_7890123456789$123456789$123456789012345678901234567890123, @@x63_7890123456789$123456789$123456789012345678901234567890123, @@z63_7890123456789$123456789$123456789012345678901234567890123 = @@z63_7890123456789$123456789$123456789012345678901234567890123 OUT, @@p63_7890123456789$123456789$123456789012345678901234567890123 = 11; +SELECT @#y63_7890123456789$123456789$123456789012345678901234567890123, @@z63_7890123456789$123456789$123456789012345678901234567890123; +go +exec p2_spexec_atatuservar +go +~~START~~ +int#!#int +20#!#22 +~~END~~ + + + + +create trigger tr17_atatuservar on t17_trigger_atatuservar for insert +as +begin +select 'trigger tr17_atatuservar' +declare @@v63_7890123456789$123456789$123456789012345678901234567890123 NVARCHAR(1000); +declare @#w63_7890123456789$123456789$123456789012345678901234567890123 NVARCHAR(max); +declare @@x63_7890123456789$123456789$123456789012345678901234567890123 NVARCHAR(1000); +SET @@v63_7890123456789$123456789$123456789012345678901234567890123 = N'SET @@z63_7890123456789$123456789$123456789012345678901234567890123 = @@p63_7890123456789$123456789$123456789012345678901234567890123 + @@p63_7890123456789$123456789$123456789012345678901234567890123'; +SET @#w63_7890123456789$123456789$123456789012345678901234567890123 = N'EXEC sp_executesql N''SET @@z63_7890123456789$123456789$123456789012345678901234567890123 = @@p63_7890123456789$123456789$123456789012345678901234567890123 + @@p63_7890123456789$123456789$123456789012345678901234567890123'', N''@@z63_7890123456789$123456789$123456789012345678901234567890123 INT OUT, @@p63_7890123456789$123456789$123456789012345678901234567890123 INT'', @@z63_7890123456789$123456789$123456789012345678901234567890123 OUTPUT, @@p63_7890123456789$123456789$123456789012345678901234567890123;'; +SET @@x63_7890123456789$123456789$123456789012345678901234567890123 = N'@@z63_7890123456789$123456789$123456789012345678901234567890123 INT OUTPUT, @@p63_7890123456789$123456789$123456789012345678901234567890123 INT'; +declare @#y63_7890123456789$123456789$123456789012345678901234567890123 INT; +declare @@z63_7890123456789$123456789$123456789012345678901234567890123 INT; +EXEC sp_executesql @@v63_7890123456789$123456789$123456789012345678901234567890123, @@x63_7890123456789$123456789$123456789012345678901234567890123, @@z63_7890123456789$123456789$123456789012345678901234567890123 = @#y63_7890123456789$123456789$123456789012345678901234567890123 OUT, @@p63_7890123456789$123456789$123456789012345678901234567890123 = 10; +EXEC sp_executesql @#w63_7890123456789$123456789$123456789012345678901234567890123, @@x63_7890123456789$123456789$123456789012345678901234567890123, @@z63_7890123456789$123456789$123456789012345678901234567890123 = @@z63_7890123456789$123456789$123456789012345678901234567890123 OUT, @@p63_7890123456789$123456789$123456789012345678901234567890123 = 11; +SELECT @#y63_7890123456789$123456789$123456789012345678901234567890123, @@z63_7890123456789$123456789$123456789012345678901234567890123; +end +go +insert t17_trigger_atatuservar values (123) +go +~~START~~ +varchar +trigger tr17_atatuservar +~~END~~ + +~~START~~ +int#!#int +20#!#22 +~~END~~ + +~~ROW COUNT: 1~~ + + +-- table variables in different types of functions +create function f1_ins_atatuservar(@@p1 int) +returns int +as +begin + declare @@tv table(a int) + insert @@tv values(@@p1) + return (select sum(a) from @@tv) +end +go +select dbo. f1_ins_atatuservar(123) +go +~~START~~ +int +123 +~~END~~ + + +create function f1_upd_atatuservar(@@p1 int) +returns int +as +begin + declare @@tv table(a int) + insert @@tv values(@@p1) + update @@tv set a = a + 1 + return (select sum(a) from @@tv) +end +go +select dbo. f1_upd_atatuservar(123) +go +~~START~~ +int +124 +~~END~~ + + +create function f1_del_atatuservar(@@p1 int) +returns int +as +begin + declare @@tv table(a int) + insert @@tv values(@@p1) + update @@tv set a = a + 1 + insert @@tv values(@@p1) + update @@tv set a = a + 1 + delete @@tv where a = 124 + return (select sum(a) from @@tv) +end +go +select dbo. f1_del_atatuservar(123) +go +~~START~~ +int +125 +~~END~~ + + +-- INSERT/UPDATE/DELETE on table variable in function +create function f3_atatuservar_upd(@@p1 int) +returns @@tv table(a int) +as +begin + insert @@tv values(@@p1) + update t set a = t.a + 1 from @@tv as t + return +end +go +select * from dbo. f3_atatuservar_upd(123) order by 1 +go +~~START~~ +int +124 +~~END~~ + + +create function f3_atatuservar_del(@@p1 int) +returns @@tv table(a int) +as +begin + insert @@tv values(@@p1) + update @@tv set a = a + 1 + insert @@tv values(@@p1) + delete t from @@tv as t where a = 124 + return +end +go +select * from dbo. f3_atatuservar_del(123) order by 1 +go +~~START~~ +int +123 +~~END~~ + + +create function f3_upd_atatuservar(@@p1 int) +returns @@tv table(a int) +as +begin + insert @@tv values(@@p1) + update t set a = t.a + 1 from t2_atatuservar, @@tv as t where t2_atatuservar.a = t.a + return +end +go +select * from dbo. f3_upd_atatuservar(123) order by 1 +go +~~START~~ +int +124 +~~END~~ + + +create function f3_del_atatuservar(@@p1 int) +returns @@tv table(a int) +as +begin + insert @@tv values(@@p1) + update @@tv set a = a + 1 + insert @@tv values(@@p1) + delete t from t2_atatuservar join @@tv as t on t2_atatuservar.a = t.a + return +end +go +select * from dbo. f3_del_atatuservar(123) order by 1 +go +~~START~~ +int +124 +~~END~~ + + +create function f4_ins_atatuservar(@#p1 int) +returns int +as +begin + declare @#tv table(a int) + insert @#tv values(@#p1) + return (select sum(a) from @#tv) +end +go +select dbo. f4_ins_atatuservar(123) +go +~~START~~ +int +123 +~~END~~ + + +create function f4_upd_atatuservar(@#p1 int) +returns int +as +begin + declare @#tv table(a int) + insert @#tv values(@#p1) + update @#tv set a = a + 1 + return (select sum(a) from @#tv) +end +go +select dbo. f4_upd_atatuservar(123) +go +~~START~~ +int +124 +~~END~~ + + +create function f4_del_atatuservar(@#p1 int) +returns int +as +begin + declare @#tv table(a int) + insert @#tv values(@#p1) + update @#tv set a = a + 1 + insert @#tv values(@#p1) + update @#tv set a = a + 1 + delete @#tv where a = 124 + return (select sum(a) from @#tv) +end +go +select dbo. f4_del_atatuservar(123) +go +~~START~~ +int +125 +~~END~~ + + +create function f5_upd_atatuservar(@#p1 int) +returns @#tv table(a int) +as +begin + insert @#tv values(@#p1) + update t set a = t.a + 1 from @#tv as t + return +end +go +select * from dbo. f5_upd_atatuservar(123) order by 1 +go +~~START~~ +int +124 +~~END~~ + + +create function f5_del_atatuservar(@#p1 int) +returns @#tv table(a int) +as +begin + insert @#tv values(@#p1) + update @#tv set a = a + 1 + insert @#tv values(@#p1) + delete t from @#tv as t where a = 124 + return +end +go +select * from dbo. f5_del_atatuservar(123) order by 1 +go +~~START~~ +int +123 +~~END~~ + + +create function f6_upd_atatuservar(@#p1 int) +returns @#tv table(a int) +as +begin + insert @#tv values(@#p1) + update t set a = t.a + 1 from t2_atatuservar, @#tv as t where t2_atatuservar.a = t.a + return +end +go +select * from dbo. f6_upd_atatuservar(123) order by 1 +go +~~START~~ +int +124 +~~END~~ + + +create function f6_del_atatuservar(@#p1 int) +returns @#tv table(a int) +as +begin + insert @#tv values(@#p1) + update @#tv set a = a + 1 + insert @#tv values(@#p1) + delete t from t2_atatuservar, @#tv as t where t2_atatuservar.a = t.a + return +end +go +select * from dbo. f6_del_atatuservar(123) order by 1 +go +~~START~~ +int +124 +~~END~~ + + +create function f7_tabvar_udf_upd_atatuservar(@@p1 int) +returns @#tv table(a int) +as +begin + insert @#tv values(@@p1) + update t set a = t.a + 1 from t2_atatuservar, @#tv as t where t2_atatuservar.a = t.a + return +end +go +select * from dbo. f7_tabvar_udf_upd_atatuservar(123) order by 1 +go +~~START~~ +int +124 +~~END~~ + + +create function f7a_tabvar_udf_upd_atatuservar(@@p1 int) +returns @#tv table(a int) +as +begin + insert @#tv values(@@p1) + update t set a = t.a + 1 from @#tv as tv, t2_atatuservar as t where tv.a = t.a + return +end +go +~~ERROR (Code: 33557097)~~ + +~~ERROR (Message: 'UPDATE' cannot be used within a function)~~ + + +create function f8_tabvar_udf_del_atatuservar(@@p1 int) +returns @#tv table(a int) +as +begin + insert @#tv values(@@p1) + update @#tv set a = a + 1 + insert @#tv values(@@p1) + delete t from @#tv as tv, t2_atatuservar as t where tv.a = t.a + return +end +go +~~ERROR (Code: 33557097)~~ + +~~ERROR (Message: 'DELETE' cannot be used within a function)~~ + + +create function f9_tabvar_udf_del_atatuservar(@p1 int) +returns @tv table(a int) +as +begin + insert @tv values(@p1) + update @tv set a = a + 1 + insert @tv values(@p1) + delete t from @tv as t inner join t2_atatuservar as tv on tv.a = t.a + return +end +go +select * from dbo.f9_tabvar_udf_del_atatuservar(123) +go +~~START~~ +int +124 +~~END~~ + + +create function f10_tabvar_udf_del_atatuservar(@#p1 int) +returns @@tv table(a int) +as +begin + insert @@tv values(@#p1) + update @@tv set a = a + 1 + insert @@tv values(@#p1) + delete t from t2_atatuservar as tv join @@tv as t on tv.a = t.a + return +end +go +select * from dbo.f10_tabvar_udf_del_atatuservar(123) +go +~~START~~ +int +124 +~~END~~ + + +create function f4_atatuservar_tabvar_in_function_upd(@#p1 int) +returns @@tv table(a int) +as +begin + insert @@tv values(@#p1) + update t set a = t.a + 1 from t2_atatuservar as t2_atatuservar left join t2_atatuservar as tv on tv.a = t2_atatuservar.a right join @@tv t on tv.a = t.a + return +end +go +select * from dbo.f4_atatuservar_tabvar_in_function_upd(123) +go +~~START~~ +int +124 +~~END~~ + + +create function f12_tabvar_udf_upd_atatuservar(@#p1 int) +returns @@tv table(a int) +as +begin + insert @@tv values(@#p1) + update t set a = t.a + 1 from @@tv as tv cross join t2_atatuservar as t + return +end +go +~~ERROR (Code: 33557097)~~ + +~~ERROR (Message: 'UPDATE' cannot be used within a function)~~ + + +create function f13_tabvar_udf_del_atatuservar(@#p1 int) +returns @@tv table(a int) +as +begin + insert @@tv values(@#p1) + update @@tv set a = a + 1 + insert @@tv values(@#p1) + delete t from @@tv as t2_atatuservar right join t2_atatuservar as tv on tv.a = t2_atatuservar.a full outer join t2_atatuservar t on tv.a = t.a + return +end +go +~~ERROR (Code: 33557097)~~ + +~~ERROR (Message: 'DELETE' cannot be used within a function)~~ + + +create function f14_del_atatuservar(@@p128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 int) +returns int +as +begin + declare @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 table(a int) + insert @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 values(@@p128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678) + update @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 set a = a + 1 + insert @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 values(@@p128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678) + update @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 set a = a + 1 + delete @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 where a = 124 + return (select sum(a) from @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678) +end +go +select dbo. f14_del_atatuservar(123) +go +~~START~~ +int +125 +~~END~~ + + +-- INSERT/UPDATE/DELETE on table variable in function +create function f15_upd_atatuservar(@@p128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 int) +returns @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 table(a int) +as +begin + insert @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 values(@@p128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678) + update t set a = t.a + 1 from @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 as t + return +end +go +select * from dbo. f15_upd_atatuservar(123) order by 1 +go +~~START~~ +int +124 +~~END~~ + + +create function f16_tabvar_udf_del_atatuservar(@#q128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 int) +returns @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 table(a int) +as +begin + insert @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 values(@#q128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678) + update @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 set a = a + 1 + insert @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 values(@#q128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678) + delete t from @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 as t2_atatuservar right join t2_atatuservar as tv on tv.a = t2_atatuservar.a full outer join t2_atatuservar t on tv.a = t.a + return +end +go +~~ERROR (Code: 33557097)~~ + +~~ERROR (Message: 'DELETE' cannot be used within a function)~~ + + +-- THROW +declare @@v int = 50001; +THROW @@v, 'Test message', 1; +go +~~ERROR (Code: 50001)~~ + +~~ERROR (Message: Test message)~~ + + +declare @@v int = 50001, @@v2 varchar(30) = 'Test message'; +THROW @@v, @@v2, 1 +go +~~ERROR (Code: 50001)~~ + +~~ERROR (Message: Test message)~~ + + +declare @@v int = 50001, @@v2 varchar(30) = 'Test message', @@v3 int=1; +THROW @@v, @@v2, @@v3 +go +~~ERROR (Code: 50001)~~ + +~~ERROR (Message: Test message)~~ + + +declare @#v int = 50001; +THROW @#v, 'Test message', 1; +go +~~ERROR (Code: 50001)~~ + +~~ERROR (Message: Test message)~~ + + +declare @#v int = 50001, @#v2 varchar(30) = 'Test message'; +THROW @#v, @#v2, 1 +go +~~ERROR (Code: 50001)~~ + +~~ERROR (Message: Test message)~~ + + +declare @#v int = 50001, @#v2 varchar(30) = 'Test message', @#v3 int=1; +THROW @#v, @#v2, @#v3 +go +~~ERROR (Code: 50001)~~ + +~~ERROR (Message: Test message)~~ + + + +create procedure p20_atatuservar @@p int +as +if @@p > 0 +begin +declare @@v int = 50001, @@v2 varchar(30) = 'Test message 1', @@v3 int=1; +THROW @@v, @@v2, @@v3 +end +declare @#v int = 50001, @#v2 varchar(30) = 'Test message 2', @#v3 int=1; +THROW @#v, @#v2, @#v3 +go +exec p20_atatuservar 1 +go +~~ERROR (Code: 50001)~~ + +~~ERROR (Message: Test message 1)~~ + +exec p20_atatuservar 0 +go +~~ERROR (Code: 50001)~~ + +~~ERROR (Message: Test message 2)~~ + + + +create trigger tr18_atatuservar on t18_trigger_atatuservar for insert +as +begin +select 'trigger tr18_atatuservar' +declare @#v int = 50001, @#v2 varchar(30) = 'Test message 2', @#v3 int=1; +THROW @#v, @#v2, @#v3 +end +go +insert t18_trigger_atatuservar values (123) +go +~~START~~ +varchar +trigger tr18_atatuservar +~~END~~ + +~~ERROR (Code: 50001)~~ + +~~ERROR (Message: Test message 2)~~ + + +declare @#v128_90123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 int = 50001, @#v128_90123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$123456789012345678901234567890123456782 varchar(30) = 'Test message', @#v128_90123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$123456789012345678901234567890123456783 int=1; +THROW @#v128_90123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678, @#v128_90123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$123456789012345678901234567890123456782, @#v128_90123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$123456789012345678901234567890123456783 +go +~~ERROR (Code: 50001)~~ + +~~ERROR (Message: Test message)~~ + + +create procedure p21_atatuservar +as +declare @#v128_90123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 int = 50001, @#v128_90123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$123456789012345678901234567890123456782 varchar(30) = 'Test message', @#v128_90123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$123456789012345678901234567890123456783 int=1; +THROW @#v128_90123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678, @#v128_90123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$123456789012345678901234567890123456782, @#v128_90123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$123456789012345678901234567890123456783 +go +exec p21_atatuservar +go +~~ERROR (Code: 50001)~~ + +~~ERROR (Message: Test message)~~ + + + +create trigger tr19_atatuservar on t19_trigger_atatuservar for insert +as +begin +select 'trigger tr19_atatuservar' +declare @#v128_90123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 int = 50001, @#v128_90123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$123456789012345678901234567890123456782 varchar(30) = 'Test message', @#v128_90123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$123456789012345678901234567890123456783 int=1; +THROW @#v128_90123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678, @#v128_90123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$123456789012345678901234567890123456782, @#v128_90123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$123456789012345678901234567890123456783 +end +go +insert t19_trigger_atatuservar values (123) +go +~~START~~ +varchar +trigger tr19_atatuservar +~~END~~ + +~~ERROR (Code: 50001)~~ + +~~ERROR (Message: Test message)~~ + + +-- DBCC CHECKIDENT +declare @@TableName varchar(100)='mytab' +DBCC CHECKIDENT (@@TableName) +go +~~ERROR (Code: 33557097)~~ + +~~ERROR (Message: DBCC CHECKIDENT does not currently support a variable for the table name)~~ + +declare @@v int=2 +DBCC CHECKIDENT ('mytab', RESEED, @@v) +go +~~ERROR (Code: 33557097)~~ + +~~ERROR (Message: DBCC CHECKIDENT does not currently support a variable for the RESEED value)~~ + + +create procedure p1_checkident_atatuservar @@TableName varchar(100), @@v int +as +DBCC CHECKIDENT (@@TableName, RESEED, @@v) +go +~~ERROR (Code: 33557097)~~ + +~~ERROR (Message: DBCC CHECKIDENT does not currently support a variable for the table name)~~ + + +create trigger tr20_atatuservar on t20_trigger_atatuservar for insert +as +begin +select 'trigger tr20_atatuservar' +declare @@TableName varchar(100)='mytab', @@v int=2 +DBCC CHECKIDENT (@@TableName, RESEED, @@v) +end +go +~~ERROR (Code: 33557097)~~ + +~~ERROR (Message: DBCC CHECKIDENT does not currently support a variable for the table name)~~ + + +declare @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 int=2 +DBCC CHECKIDENT ('mytab', RESEED, @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678) +go +~~ERROR (Code: 33557097)~~ + +~~ERROR (Message: DBCC CHECKIDENT does not currently support a variable for the RESEED value)~~ + + +create procedure p2_checkident_atatuservar @@t128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 varchar(100), @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 int +as +DBCC CHECKIDENT (@@t128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678, RESEED, @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678) +go +~~ERROR (Code: 33557097)~~ + +~~ERROR (Message: DBCC CHECKIDENT does not currently support a variable for the table name)~~ + + +create trigger tr21_atatuservar on t21_trigger_atatuservar for insert +as +begin +select 'trigger tr21_atatuservar' +declare @@t128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 varchar(100)='mytab', @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 int=2 +DBCC CHECKIDENT (@@t128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678, RESEED, @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678) +end +go +~~ERROR (Code: 33557097)~~ + +~~ERROR (Message: DBCC CHECKIDENT does not currently support a variable for the table name)~~ + + +-- transaction name +declare @@V varchar(50) = 'myxact' +begin transaction @@v +insert t3_atatuservar values(234) +declare @#V varchar(50) = 'mysavept' +save transaction @#v +insert t3_atatuservar values(345) +set @#V = 'mysavept' +rollback transaction @#v +insert t3_atatuservar values(567) +commit transaction @@v +go +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +select * from t3_atatuservar +go +~~START~~ +int +123 +234 +567 +~~END~~ + + +create procedure p1_xactname_atatuservar +as +declare @@V varchar(50) = 'myxact' +delete t3_atatuservar where a > 123 +begin transaction @@v +insert t3_atatuservar values(234) +declare @#V varchar(50) = 'mysavept' +save transaction @#v +insert t3_atatuservar values(345) +set @#V = 'mysavept' +rollback transaction @#v +insert t3_atatuservar values(567) +commit transaction @@v +select * from t3_atatuservar +go +exec p1_xactname_atatuservar +go +~~ROW COUNT: 2~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~START~~ +int +123 +234 +567 +~~END~~ + + +delete t3_atatuservar where a > 123 +declare @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 varchar(50) = 'myxact' +begin transaction @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 +insert t3_atatuservar values(234) +declare @#v128_90123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 varchar(50) = 'mysavept' +save transaction @#v128_90123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 +insert t3_atatuservar values(345) +set @#v128_90123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 = 'mysavept' +rollback transaction @#v128_90123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 +insert t3_atatuservar values(567) +commit transaction @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 +go +~~ROW COUNT: 2~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +select * from t3_atatuservar +go +~~START~~ +int +123 +234 +567 +~~END~~ + + +create procedure p2_xactname_atatuservar +as +declare @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 varchar(50) = 'myxact' +delete t3_atatuservar where a > 123 +begin transaction @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 +insert t3_atatuservar values(234) +declare @#v128_90123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 varchar(50) = 'mysavept' +save transaction @#v128_90123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 +insert t3_atatuservar values(345) +set @#v128_90123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 = 'mysavept' +rollback transaction @#v128_90123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 +insert t3_atatuservar values(567) +commit transaction @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 +select * from t3_atatuservar +go +exec p2_xactname_atatuservar +go +~~ROW COUNT: 2~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~START~~ +int +123 +234 +567 +~~END~~ + + +-- cursor variable in T-SQL batch, stored procedure, trigger +declare @#v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 varchar(20) +declare @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 cursor +set @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 = cursor for select a from t_checkident_atatuservar order by n +open @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 +while 1=1 +begin + fetch from @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 into @#v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 + if @@fetch_status <> 0 break + select @#v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 +end +close @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 +deallocate @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 +go +~~START~~ +varchar +one +~~END~~ + +~~START~~ +varchar +two +~~END~~ + +~~START~~ +varchar +three +~~END~~ + +~~START~~ +varchar +four +~~END~~ + + +create procedure p1_cursor_atatuservar +as +begin + declare @#v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 varchar(20) + declare @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 cursor + set @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 = cursor for select a from t_checkident_atatuservar order by n + open @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 + while 1=1 + begin + fetch from @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 into @#v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 + if @@fetch_status <> 0 break + select @#v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 + end + close @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 + deallocate @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 +end +go +exec p1_cursor_atatuservar +go +~~START~~ +varchar +one +~~END~~ + +~~START~~ +varchar +two +~~END~~ + +~~START~~ +varchar +three +~~END~~ + +~~START~~ +varchar +four +~~END~~ + + + +create trigger tr22_atatuservar on t22_trigger_atatuservar for insert +as +begin + select 'trigger tr22_atatuservar' + declare @@v128_90123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 varchar(20) + declare @#v128_90123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 cursor + set @#v128_90123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 = cursor for select a from t_checkident_atatuservar order by n + open @#v128_90123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 + while 1=1 + begin + fetch from @#v128_90123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 into @@v128_90123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 + if @@fetch_status <> 0 break + select @@v128_90123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 + end + close @#v128_90123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 + deallocate @#v128_90123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 +end +go +insert t22_trigger_atatuservar values (123) +go +~~START~~ +varchar +trigger tr22_atatuservar +~~END~~ + +~~START~~ +varchar +one +~~END~~ + +~~START~~ +varchar +two +~~END~~ + +~~START~~ +varchar +three +~~END~~ + +~~START~~ +varchar +four +~~END~~ + +~~ROW COUNT: 1~~ + +set quoted_identifier on +go + +-- user-defined variables that were previously mapped to internal sys. functions, e.g. sys.rand() +declare @@xact_state int=123 select @@xact_state, xact_state() +go +~~START~~ +int#!#smallint +123#!#0 +~~END~~ + +declare @@error_line int=123 select @@error_line, error_line() +go +~~START~~ +int#!#int +123#!# +~~END~~ + +declare @@error_message int=123 select @@error_message, error_message() +go +~~START~~ +int#!#nvarchar +123#!# +~~END~~ + +declare @@error_number int=123 select @@error_number, error_number() +go +~~START~~ +int#!#int +123#!# +~~END~~ + +declare @@error_procedure int=123 select @@error_procedure, error_procedure() +go +~~START~~ +int#!#nvarchar +123#!# +~~END~~ + +declare @@error_severity int=123 select @@error_severity, error_severity() +go +~~START~~ +int#!#int +123#!# +~~END~~ + +declare @@error_state int=123 select @@error_state, error_state() +go +~~START~~ +int#!#int +123#!# +~~END~~ + +declare @@db_id int=123 select @@db_id, db_id('master') +go +~~START~~ +int#!#smallint +123#!#1 +~~END~~ + +declare @@db_name int=123 select @@db_name, db_name() +go +~~START~~ +int#!#nvarchar +123#!#master +~~END~~ + +declare @@exp int=123 select @@exp, exp(1) +go +~~START~~ +int#!#float +123#!#2.718281828459045 +~~END~~ + +declare @@sign int=123 select @@sign, sign(1) +go +~~START~~ +int#!#int +123#!#1 +~~END~~ + +declare @@lock_timeout int=123 select @@lock_timeout, lock_timeout() +go +~~START~~ +int#!#int +-1#!#-1 +~~END~~ + +declare @@rand int=123 select @@rand, case when rand() <= 0 then 'too small, <= 0' when rand() >= 1 then ' too large, >= 1' else 'correct, >0 && <1' end +go +~~START~~ +int#!#varchar +123#!#correct, >0 && <1 +~~END~~ + +declare @@trigger_nestlevel int=123 select @@trigger_nestlevel, trigger_nestlevel() +go +~~START~~ +int#!#int +123#!#0 +~~END~~ + +declare @@atn2 int=123 select @@atn2, atn2(1,0) +go +~~START~~ +int#!#float +123#!#1.5707963267948966 +~~END~~ + +declare @@datepart int=123 select @@datepart, datepart(yy, '2024-11-12') +go +~~START~~ +int#!#int +123#!#2024 +~~END~~ + +declare @@datediff int=123 select @@datediff, datediff(dd, '2024-11-12', '2024-12-13') +go +~~START~~ +int#!#int +123#!#31 +~~END~~ + +declare @@datediff_big int=123 select @@datediff_big, datediff(dd, '2024-11-12', '2024-12-13') +go +~~START~~ +int#!#int +123#!#31 +~~END~~ + +declare @@dateadd int=123 select @@dateadd, datediff(dd, 3, '2024-11-12') +go +~~START~~ +int#!#int +123#!#45603 +~~END~~ + +declare @@datename int=123 select @@datename, datename(dw, '2024-11-12') +go +~~START~~ +int#!#text +123#!#Tuesday +~~END~~ + + +create proc p1_sysfunctions_atatuservar +as +begin +declare @@xact_state int=123 select @@xact_state, xact_state() +declare @@error_line int=123 select @@error_line, error_line() +declare @@error_message int=123 select @@error_message, error_message() +declare @@error_number int=123 select @@error_number, error_number() +declare @@error_procedure int=123 select @@error_procedure, error_procedure() +declare @@error_state int=123 select @@error_state, error_state() +declare @@db_id int=123 select @@db_id, db_id('master') +declare @@db_name int=123 select @@db_name, db_name() +declare @@exp int=123 select @@exp, exp(1) +declare @@sign int=123 select @@sign, sign(1) +declare @@lock_timeout int=123 select @@lock_timeout, lock_timeout() +declare @@rand int=123 select @@rand, case when rand() <= 0 then 'too small, <= 0' when rand() >= 1 then ' too large, >= 1' else 'correct, >0 && <1' end +declare @@trigger_nestlevel int=123 select @@trigger_nestlevel, trigger_nestlevel() +declare @@atn2 int=123 select @@atn2, atn2(1,0) +declare @@datepart int=123 select @@datepart, datepart(yy, '2024-11-12') +declare @@datediff int=123 select @@datediff, datediff(dd, '2024-11-12', '2024-12-13') +declare @@datediff_big int=123 select @@datediff_big, datediff(dd, '2024-11-12', '2024-12-13') +declare @@dateadd int=123 select @@dateadd, datediff(dd, 3, '2024-11-12') +declare @@datename int=123 select @@datename, datename(dw, '2024-11-12') +end +go +exec p1_sysfunctions_atatuservar +go +~~START~~ +int#!#smallint +123#!#0 +~~END~~ + +~~START~~ +int#!#int +123#!# +~~END~~ + +~~START~~ +int#!#nvarchar +123#!# +~~END~~ + +~~START~~ +int#!#int +123#!# +~~END~~ + +~~START~~ +int#!#nvarchar +123#!# +~~END~~ + +~~START~~ +int#!#int +123#!# +~~END~~ + +~~START~~ +int#!#smallint +123#!#1 +~~END~~ + +~~START~~ +int#!#nvarchar +123#!#master +~~END~~ + +~~START~~ +int#!#float +123#!#2.718281828459045 +~~END~~ + +~~START~~ +int#!#int +123#!#1 +~~END~~ + +~~START~~ +int#!#int +-1#!#-1 +~~END~~ + +~~START~~ +int#!#varchar +123#!#correct, >0 && <1 +~~END~~ + +~~START~~ +int#!#int +123#!#0 +~~END~~ + +~~START~~ +int#!#float +123#!#1.5707963267948966 +~~END~~ + +~~START~~ +int#!#int +123#!#2024 +~~END~~ + +~~START~~ +int#!#int +123#!#31 +~~END~~ + +~~START~~ +int#!#int +123#!#31 +~~END~~ + +~~START~~ +int#!#int +123#!#45603 +~~END~~ + +~~START~~ +int#!#text +123#!#Tuesday +~~END~~ + + +create function f1_sysfunctions_atatuservar() returns int +as +begin +declare @@xact_state int=123 +declare @@error_line int=123 +declare @@error_message int=123 +declare @@error_number int=123 +declare @@error_procedure int=123 +declare @@error_state int=123 +declare @@db_id int=123 +declare @@db_name int=123 +declare @@exp int=123 +declare @@sign int=123 +declare @@lock_timeout int=123 +declare @@rand int=123 +declare @@trigger_nestlevel int=123 +declare @@atn2 int=123 +declare @@datepart int=123 +declare @@datediff int=123 +declare @@datediff_big int=123 +declare @@dateadd int=123 +declare @@datename int=123 +return @@xact_state + @@error_line + @@error_message + @@error_number + @@error_procedure + @@error_state + @@db_id + @@db_name + @@exp + @@sign + @@lock_timeout + @@rand + @@trigger_nestlevel + @@atn2 + @@datepart + @@datediff + @@datediff_big + @@dateadd + @@datename +end +go +select dbo.f1_sysfunctions_atatuservar() +go +~~START~~ +int +2213 +~~END~~ + + +create trigger tr1_sysfunctions_atatuservar on t23_trigger_atatuservar for insert as +begin +declare @@xact_state int=123 select @@xact_state, xact_state() +declare @@error_line int=123 select @@error_line, error_line() +declare @@error_message int=123 select @@error_message, error_message() +declare @@error_number int=123 select @@error_number, error_number() +declare @@error_procedure int=123 select @@error_procedure, error_procedure() +declare @@error_state int=123 select @@error_state, error_state() +declare @@db_id int=123 select @@db_id, db_id('master') +declare @@db_name int=123 select @@db_name, db_name() +declare @@exp int=123 select @@exp, exp(1) +declare @@sign int=123 select @@sign, sign(1) +declare @@lock_timeout int=123 select @@lock_timeout, lock_timeout() +declare @@rand int=123 select @@rand, case when rand() <= 0 then 'too small, <= 0' when rand() >= 1 then ' too large, >= 1' else 'correct, >0 && <1' end +declare @@trigger_nestlevel int=123 select @@trigger_nestlevel, trigger_nestlevel() +declare @@atn2 int=123 select @@atn2, atn2(1,0) +declare @@datepart int=123 select @@datepart, datepart(yy, '2024-11-12') +declare @@datediff int=123 select @@datediff, datediff(dd, '2024-11-12', '2024-12-13') +declare @@datediff_big int=123 select @@datediff_big, datediff(dd, '2024-11-12', '2024-12-13') +declare @@dateadd int=123 select @@dateadd, datediff(dd, 3, '2024-11-12') +declare @@datename int=123 select @@datename, datename(dw, '2024-11-12') +end +go +insert t23_trigger_atatuservar values(1) +go +~~START~~ +int#!#smallint +123#!#1 +~~END~~ + +~~START~~ +int#!#int +123#!# +~~END~~ + +~~START~~ +int#!#nvarchar +123#!# +~~END~~ + +~~START~~ +int#!#int +123#!# +~~END~~ + +~~START~~ +int#!#nvarchar +123#!# +~~END~~ + +~~START~~ +int#!#int +123#!# +~~END~~ + +~~START~~ +int#!#smallint +123#!#1 +~~END~~ + +~~START~~ +int#!#nvarchar +123#!#master +~~END~~ + +~~START~~ +int#!#float +123#!#2.718281828459045 +~~END~~ + +~~START~~ +int#!#int +123#!#1 +~~END~~ + +~~START~~ +int#!#int +-1#!#-1 +~~END~~ + +~~START~~ +int#!#varchar +123#!#correct, >0 && <1 +~~END~~ + +~~START~~ +int#!#int +123#!#1 +~~END~~ + +~~START~~ +int#!#float +123#!#1.5707963267948966 +~~END~~ + +~~START~~ +int#!#int +123#!#2024 +~~END~~ + +~~START~~ +int#!#int +123#!#31 +~~END~~ + +~~START~~ +int#!#int +123#!#31 +~~END~~ + +~~START~~ +int#!#int +123#!#45603 +~~END~~ + +~~START~~ +int#!#text +123#!#Tuesday +~~END~~ + +~~ROW COUNT: 1~~ + diff --git a/test/JDBC/expected/unquoted_string-vu-prepare.out b/test/JDBC/expected/unquoted_string-vu-prepare.out index 8025a74a6e..6c8e5b2547 100644 --- a/test/JDBC/expected/unquoted_string-vu-prepare.out +++ b/test/JDBC/expected/unquoted_string-vu-prepare.out @@ -119,15 +119,6 @@ go ~~ERROR (Message: column "@p1" does not exist)~~ --- should raise error: -create procedure unqStr_proc_6 @p1 varchar(20) = @@myvar -as select @p1 -go -~~ERROR (Code: 33557097)~~ - -~~ERROR (Message: function sys.myvar() does not exist)~~ - - create procedure unqStr_proc_7 @p1 varchar(20) = N'aB"c''D', @p2 varchar(20) = dEfg, @p3 varchar(20) = "x'Y""z" as select @p1, @p2, @p3 go diff --git a/test/JDBC/expected/unquoted_string-vu-verify.out b/test/JDBC/expected/unquoted_string-vu-verify.out index 12969bad57..3143be75c4 100644 --- a/test/JDBC/expected/unquoted_string-vu-verify.out +++ b/test/JDBC/expected/unquoted_string-vu-verify.out @@ -713,6 +713,15 @@ go Position: 71 Server SQLState: 42703)~~ +-- should raise an error since variable is not defined +create procedure unqStr_proc_6 @p1 varchar(20) = @@myvar +as select @p1 +go +~~ERROR (Code: 0)~~ + +~~ERROR (Message: ERROR: syntax error at or near "@" + Position: 87 + Server SQLState: 42601)~~ -- should print 'aBc': select unqStr_pgfunc_1('aBc'); diff --git a/test/JDBC/input/atatuservar-vu-cleanup.sql b/test/JDBC/input/atatuservar-vu-cleanup.sql new file mode 100644 index 0000000000..37f2c4171f --- /dev/null +++ b/test/JDBC/input/atatuservar-vu-cleanup.sql @@ -0,0 +1,167 @@ +drop procedure p1_atatuservar +go +drop procedure p2_atatuservar +go +drop function f1_atatuservar +go +drop function f2_atatuservar +go +drop procedure p3_atatuservar +go +drop function f3_atatuservar +go +drop procedure p4_atatuservar +go +drop procedure p19_atatuservar +go +drop procedure p5_atatuservar +go +drop procedure p6_atatuservar +go +drop procedure p7_atatuservar +go +drop procedure p8_atatuservar +go +drop procedure p9_atatuservar +go +drop procedure p10_atatuservar +go +drop procedure p11_atatuservar +go +drop function f4_atatuservar +go +drop procedure p22_atatuservar +go +drop procedure p12_atatuservar +go +drop procedure p13_atatuservar +go +drop procedure p14_atatuservar +go +drop function f1_atatuservar_extra_long_name_here +go +drop function f2_atatuservar_extra_long_name_here +go +drop procedure p15_atatuservar +go +drop procedure p16_atatuservar +go +drop procedure p17_atatuservar +go +drop procedure p18_atatuservar +go +drop procedure p1_raiserror_atatuservar +go +drop procedure p1_spexec_atatuservar +go +drop procedure p2_spexec_atatuservar +go +drop function f1_ins_atatuservar +go +drop function f1_upd_atatuservar +go +drop function f1_del_atatuservar +go +drop function f3_atatuservar_upd +go +drop function f3_atatuservar_del +go +drop function f3_upd_atatuservar +go +drop function f3_del_atatuservar +go +drop function f4_ins_atatuservar +go +drop function f4_upd_atatuservar +go +drop function f4_del_atatuservar +go +drop function f5_upd_atatuservar +go +drop function f5_del_atatuservar +go +drop function f6_upd_atatuservar +go +drop function f6_del_atatuservar +go +drop function f7_tabvar_udf_upd_atatuservar +go +drop function f9_tabvar_udf_del_atatuservar +go +drop function f10_tabvar_udf_del_atatuservar +go +drop function f4_atatuservar_tabvar_in_function_upd +go +drop function f14_del_atatuservar +go +drop function f15_upd_atatuservar +go +drop procedure p20_atatuservar +go +drop procedure p21_atatuservar +go +drop procedure p1_xactname_atatuservar +go +drop procedure p2_xactname_atatuservar +go + +drop table t1_atatuservar +go +drop table t2_atatuservar +go +drop table t3_atatuservar +go +drop table t_checkident_atatuservar +go +drop table t1_trigger_atatuservar +go +drop table t2_trigger_atatuservar +go +drop table t3_trigger_atatuservar +go +drop table t4_trigger_atatuservar +go +drop table t5_trigger_atatuservar +go +drop table t6_trigger_atatuservar +go +drop table t7_trigger_atatuservar +go +drop table t8_trigger_atatuservar +go +drop table t9_trigger_atatuservar +go +drop table t10_trigger_atatuservar +go +drop table t11_trigger_atatuservar +go +drop table t12_trigger_atatuservar +go +drop table t13_trigger_atatuservar +go +drop table t14_trigger_atatuservar +go +drop table t15_trigger_atatuservar +go +drop table t16_trigger_atatuservar +go +drop table t17_trigger_atatuservar +go +drop table t18_trigger_atatuservar +go +drop table t19_trigger_atatuservar +go +drop table t20_trigger_atatuservar +go +drop table t21_trigger_atatuservar +go +drop table t22_trigger_atatuservar +go +drop table t23_trigger_atatuservar +go +drop procedure p1_cursor_atatuservar +go +drop procedure p1_sysfunctions_atatuservar +go +drop function f1_sysfunctions_atatuservar +go \ No newline at end of file diff --git a/test/JDBC/input/atatuservar-vu-prepare.sql b/test/JDBC/input/atatuservar-vu-prepare.sql new file mode 100644 index 0000000000..00efc2b954 --- /dev/null +++ b/test/JDBC/input/atatuservar-vu-prepare.sql @@ -0,0 +1,62 @@ +create table t1_atatuservar (c varchar(20)) +insert t1_atatuservar values ('row 1'), ('row 2'), ('row 3') +go +create table t2_atatuservar (a int) +insert t2_atatuservar values(123) +go +create table t3_atatuservar (a int) +insert t3_atatuservar values(123) +go +create table t_checkident_atatuservar (a varchar(20), n int identity) +go +insert t_checkident_atatuservar values('one') +insert t_checkident_atatuservar values('two') +insert t_checkident_atatuservar values('three') +insert t_checkident_atatuservar values('four') +go +create table t1_trigger_atatuservar (a int) +go +create table t2_trigger_atatuservar (a int) +go +create table t3_trigger_atatuservar (a int) +go +create table t4_trigger_atatuservar (a int) +go +create table t5_trigger_atatuservar (a int) +go +create table t6_trigger_atatuservar (a int) +go +create table t7_trigger_atatuservar (a int) +go +create table t8_trigger_atatuservar (a int) +go +create table t9_trigger_atatuservar (a int) +go +create table t10_trigger_atatuservar(a int) +go +create table t11_trigger_atatuservar(a int) +go +create table t12_trigger_atatuservar(a int) +go +create table t13_trigger_atatuservar(a int) +go +create table t14_trigger_atatuservar(a int) +go +create table t15_trigger_atatuservar(a int) +go +create table t16_trigger_atatuservar(a int) +go +create table t17_trigger_atatuservar(a int) +go +create table t18_trigger_atatuservar(a int) +go +create table t19_trigger_atatuservar(a int) +go +create table t20_trigger_atatuservar(a int) +go +create table t21_trigger_atatuservar(a int) +go +create table t22_trigger_atatuservar(a int) +go +create table t23_trigger_atatuservar(a int) +go \ No newline at end of file diff --git a/test/JDBC/input/atatuservar-vu-verify.sql b/test/JDBC/input/atatuservar-vu-verify.sql new file mode 100644 index 0000000000..dd7e1b2f1c --- /dev/null +++ b/test/JDBC/input/atatuservar-vu-verify.sql @@ -0,0 +1,1842 @@ +-- simple variables in T-SQL batch +set quoted_identifier off +go +declare @@v int = 2 select @@v +go +declare @#v int = 2 select @#v +go +declare @@@$$@@@@v int = 2 select @@@$$@@@@v +go +declare @@@$$@@@@v##### int = 2 select @@@$$@@@@v##### +go +declare @@@$$@@@@#####v int = 2 select @@@$$@@@@#####v +go +declare @#############v int = 2 select @#############v +go + +-- 63 long +declare @@v63_7890123456789$123456789$123456789012345678901234567890123 int = 2 +select @@v63_7890123456789$123456789$123456789012345678901234567890123 +go +-- 64 long +declare @@v64_7890123456789$123456789$1234567890123456789012345678901234 int = 2 +select @@v64_7890123456789$123456789$1234567890123456789012345678901234 +go +-- maximum length in T-SQL is 128 +declare @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 int = 2 +select @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 +go + +declare @#v63_7890123456789$123456789$123456789012345678901234567890123 int = 2 +select @#v63_7890123456789$123456789$123456789012345678901234567890123 +go +declare @#v64_7890123456789$123456789$1234567890123456789012345678901234 int = 2 +select @#v64_7890123456789$123456789$1234567890123456789012345678901234 +go +declare @#v128_90123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 int = 2 +select @#v128_90123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 +go +declare @@v128_@@@$$@@@@$$@########@@@$$@@@@$$@@@@$$@@@###__##@@@$$@@@@$$@@@@$$@@@@$$@@@______@@@$$@@@@$$@@@@$$@@@###########@@@$$@@@@@@ int = 2 +select @@v128_@@@$$@@@@$$@########@@@$$@@@@$$@@@@$$@@@###__##@@@$$@@@@$$@@@@$$@@@@$$@@@______@@@$$@@@@$$@@@@$$@@@###########@@@$$@@@@@@ +go + +-- referencing global @@variable, in T-SQL batch, procedure, trigger and function +declare @@v int +select 1 +set @@v = @@rowcount +select @@v +go + +declare @@@$$@@@@v int, @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 int +select 1 +set @@@$$@@@@v = @@rowcount +set @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 = @@pgerror +select @@@$$@@@@v, @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 +go + +declare @@v63_7890123456789$123456789$123456789012345678901234567890123 int +set @@v63_7890123456789$123456789$123456789012345678901234567890123 = @@rowcount +select @@v63_7890123456789$123456789$123456789012345678901234567890123 +go + +declare @#v64_7890123456789$123456789$1234567890123456789012345678901234 int +set @#v64_7890123456789$123456789$1234567890123456789012345678901234 = @@rowcount +select @#v64_7890123456789$123456789$1234567890123456789012345678901234 +go + +declare @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 int +set @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 = @@rowcount +select @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 +go + +create procedure p1_atatuservar +as +declare @@v int +select 1 +set @@v = @@rowcount +select @@v + +declare @#v int +select 1 +set @#v = @@rowcount +select @#v +go +exec p1_atatuservar +go + +create procedure p2_atatuservar +as +declare @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 int, @@p128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 int +select 1 +set @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 = @@rowcount +select @@p128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 = @@pgerror +select @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678, @@p128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 + +declare @#v64_7890123456789$123456789$1234567890123456789012345678901234 int +select 1 +set @#v64_7890123456789$123456789$1234567890123456789012345678901234 = @@rowcount +select @#v64_7890123456789$123456789$1234567890123456789012345678901234 +go +exec p2_atatuservar +go + +create trigger tr1_atatuservar on t1_trigger_atatuservar for insert +as +begin +select 'trigger tr1_atatuservar' +declare @@v int +select 1 +set @@v = @@rowcount +select @@v + +declare @#v int +select 1 +set @#v = @@rowcount +select @#v +end +go +insert t1_trigger_atatuservar values (123) +go + +create trigger tr2_atatuservar on t2_trigger_atatuservar for insert +as +begin +select 'trigger tr2_atatuservar' + +declare @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 int +select 1 +set @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 = @@rowcount +select @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 + +declare @#v64_7890123456789$123456789$1234567890123456789012345678901234 int +select 1 +set @#v64_7890123456789$123456789$1234567890123456789012345678901234 = @@rowcount +select @#v64_7890123456789$123456789$1234567890123456789012345678901234 +end +go +insert t2_trigger_atatuservar values (123) +go + +create function f1_atatuservar() returns int +as +begin +declare @@v int, @@v2 int +select @@v2 = count(*) from t1_atatuservar +set @@v = @@rowcount + +declare @#v int, @#v2 int +select @#v2 = count(*) from t1_atatuservar +set @#v = @@rowcount + +return @@v * @#v +end +go +select dbo.f1_atatuservar() +go + +create function f2_atatuservar() returns int +as +begin +declare @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 int, @#v64_7890123456789$123456789$1234567890123456789012345678901234 int +select @#v64_7890123456789$123456789$1234567890123456789012345678901234 = count(*) from t1_atatuservar +set @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 = @@rowcount + +return @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 * @#v64_7890123456789$123456789$1234567890123456789012345678901234 +end +go +select dbo.f2_atatuservar() +go + +create trigger tr3_atatuservar on t3_trigger_atatuservar for insert +as +begin +select 'trigger tr3_atatuservar' +declare @@v int, @@v2 int +select @@v2 = count(*) from t1_atatuservar +set @@v = @@rowcount +select @@v + +declare @#v int, @#v2 int +select @#v2 = count(*) from t1_atatuservar +set @#v = @@rowcount +select @#v +end +go +insert t3_trigger_atatuservar values (123) +go + +-- assuming @@servername is BABELFISH +declare @v varchar(50) +set @v = @@servername +select @v +go + +declare @v int +set @v = len(@@servername) +select @v +go + +create procedure p3_atatuservar +as +declare @v varchar(50) +set @v = @@servername +select @v + +declare @v2 int +set @v2 = len(@@servername) +select @v2 +go +exec p3_atatuservar +go + +create function f3_atatuservar(@#p1 varchar(30)) returns int +as +begin +declare @@v int +set @@v = len(@@servername) +return @@v * len(@#p1) +end +go +select dbo.f3_atatuservar(@@servername) +go + +create trigger tr4_atatuservar on t4_trigger_atatuservar for insert +as +begin +select 'trigger tr4_atatuservar' +declare @@v int +set @@v = len(@@servername) +select @@v +end +go +insert t4_trigger_atatuservar values (123) +go + +-- procedure call with named/unnamed arguments, output parameters, return status, in T-SQL batch, procedure, trigger +create procedure p4_atatuservar @@p1 int +as select @@p1 +return @@p1*-1 +go +exec p4_atatuservar 123 +go +exec p4_atatuservar @@p1=123 +go +declare @@v int = 987 +exec p4_atatuservar @@p1=@@v +go +declare @@v int +exec @@v = p4_atatuservar @@p1=123 +select @@v +go + +create procedure p19_atatuservar @#p19_atatuservar int +as select @#p19_atatuservar +return @#p19_atatuservar*-1 +go +exec p19_atatuservar @@max_precision +go +exec p19_atatuservar @#p19_atatuservar=@@max_precision +go +declare @#v int = @@max_precision +exec p19_atatuservar @#p19_atatuservar=@#v +go +declare @#v int +exec @#v = p19_atatuservar @#p19_atatuservar=@@max_precision +select @#v +go + +create procedure p5_atatuservar +as +declare @@v int +exec @@v = p4_atatuservar @@p1=123 +select @@v + +declare @#v int +exec @#v = p4_atatuservar @@p1=123 +select @#v +go +exec p5_atatuservar +go + +create procedure p6_atatuservar @@p4 int output +as +set @@p4 *= 2 +select @@p4 +return @@p4*-1 +go +declare @#v1 int, @@v2 int = 123 +exec @#v1 = p6_atatuservar @@v2 +select @#v1, @@v2 +go + +declare @#v1 int, @@v2 int = 123 +exec @#v1 = p6_atatuservar @@p4 = @@v2 out +select @#v1, @@v2 +go + +create procedure p7_atatuservar +as +declare @#v1 int, @@v2 int = 123 +exec @#v1 = p6_atatuservar @@v2 output +select @#v1, @@v2 +go + +create procedure p8_atatuservar @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 int output +as +set @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 *= 2 +select @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 +return @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678*-1 +go +declare @#v64_7890123456789$123456789$1234567890123456789012345678901234 int, @@v63_7890123456789$123456789$123456789012345678901234567890123 int = 123 +exec @#v64_7890123456789$123456789$1234567890123456789012345678901234 = p8_atatuservar @@v63_7890123456789$123456789$123456789012345678901234567890123 +select @#v64_7890123456789$123456789$1234567890123456789012345678901234, @@v63_7890123456789$123456789$123456789012345678901234567890123 +go + +declare @#v64_7890123456789$123456789$1234567890123456789012345678901234 int, @@v63_7890123456789$123456789$123456789012345678901234567890123 int = 123 +exec @#v64_7890123456789$123456789$1234567890123456789012345678901234 = p8_atatuservar @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 = @@v63_7890123456789$123456789$123456789012345678901234567890123 out +select @#v64_7890123456789$123456789$1234567890123456789012345678901234, @@v63_7890123456789$123456789$123456789012345678901234567890123 +go + +create procedure p9_atatuservar +as +declare @#v64_7890123456789$123456789$1234567890123456789012345678901234 int, @@v63_7890123456789$123456789$123456789012345678901234567890123 int = 123 +exec @#v64_7890123456789$123456789$1234567890123456789012345678901234 = p8_atatuservar @@v63_7890123456789$123456789$123456789012345678901234567890123 output +select @#v64_7890123456789$123456789$1234567890123456789012345678901234, @@v63_7890123456789$123456789$123456789012345678901234567890123 +go + +create trigger tr5_atatuservar on t5_trigger_atatuservar for insert +as +begin +select 'trigger tr5_atatuservar' +declare @@v int +exec @@v = p4_atatuservar @@p1=123 +select @@v + +declare @#v int +exec @#v = p4_atatuservar @@p1=123 +select @#v +end +go +insert t5_trigger_atatuservar values (123) +go + +create trigger tr6_atatuservar on t6_trigger_atatuservar for insert +as +begin +select 'trigger tr6_atatuservar' + +declare @#v64_7890123456789$123456789$1234567890123456789012345678901234 int, @@v63_7890123456789$123456789$123456789012345678901234567890123 int = 123 +exec @#v64_7890123456789$123456789$1234567890123456789012345678901234 = p8_atatuservar @@v63_7890123456789$123456789$123456789012345678901234567890123 output +select @#v64_7890123456789$123456789$1234567890123456789012345678901234, @@v63_7890123456789$123456789$123456789012345678901234567890123 +end +go +insert t6_trigger_atatuservar values (123) +go + +-- misc variable usage in expressions +declare @#v int = 3 +set @#v *= -1 +set @#v = @#v + 10*@#v +select @#v = @#v + 1 +if @#v > 0 select 'positive' else select 'negative' +select @#v, @#V, '@#v' +print @#v -- PRINT output not captured by JDBC tests +go + +declare @@v int = 3 +set @@v *= -1 +set @@v = @@v + 10*@@v +select @@v = @@v + 1 +if @@v > 0 select 'positive' else select 'negative' +select @@v , @@V, '@@v' +print @@V -- PRINT output not captured by JDBC tests +go + +create procedure p10_atatuservar +as +declare @#v int = 3 +set @#v *= -1 +set @#v = @#v + 10*@#v +select @#v = @#v + 1 +if @#v > 0 select 'positive' else select 'negative' +select @#v, @#V, '@#v' +print @#v -- PRINT output not captured by JDBC tests + +declare @@v int = 3 +set @@v *= -1 +set @@v = @@v + 10*@@v +select @@v = @@v + 1 +if @@v > 0 select 'positive' else select 'negative' +select @@v , @@V, '@@v' +print @@V -- PRINT output not captured by JDBC tests +go +exec p10_atatuservar +go + +declare @@c varchar(20)='abcd', @#v1 int=2, @v2@# int=1 +select substring(@@c, @#v1, @v2@#) +go + +declare @@v1 varchar(20)='row 1', @#v2 varchar(20)='row 3' +select * from t1_atatuservar where c in (@@v1, @#v2) order by 1 +go + +declare @@v1 varchar(20)='1', @#v2 varchar(20)='3' +select * from t1_atatuservar where c like '%'+@@v1 or c like '%'+@#v2 order by 1 +go + +create procedure p11_atatuservar +as +declare @@c varchar(20)='abcd', @#v int=2, @v@# int=1 +select substring(@@c, @#v, @v@#) + +declare @@v1 varchar(20)='row 1', @#v1 varchar(20)='row 3' +select * from t1_atatuservar where c in (@@v1, @#v1) order by 1 + +declare @@v2 varchar(20)='1', @#v2 varchar(20)='3' +select * from t1_atatuservar where c like '%'+@@v2 or c like '%'+@#v2 order by 1 +go +exec p11_atatuservar +go + +create function f4_atatuservar (@@p1 int, @#p2 int, @p5_atatuservar int) +returns table +as +return (select @@p1*@#p2*@p5_atatuservar as x where @@p1 = 123 or @#p2 = 10 or @p5_atatuservar = 1) +go +select * from dbo.f4_atatuservar(123, 20, 2) +go +select * from dbo.f4_atatuservar(345, 10, 2) +go +select * from dbo.f4_atatuservar(345, 20, 1) +go + +declare @@c varchar(30), @#v int=1, @#v2 int=2 +set @@c = case when @#v > 0 then '> zero' else '<- zero' end +select @@c +set @@c = case @#v when 0 then 'zero' else '<> zero' end +select @@c +set @@c = case @#v when 0 then 'zero' when @#v2 then 'case 2' else 'something else' end +select @@c +set @@c = case @#v+@#v when 0 then 'zero' when @#v2 then 'case 2' else 'something else' end +select @@c +go + +create procedure p22_atatuservar +as +declare @@c varchar(30), @#v int=1, @#v2 int=2 +set @@c = case when @#v > 0 then '> zero' else '<- zero' end +select @@c +set @@c = case @#v when 0 then 'zero' else '<> zero' end +select @@c +set @@c = case @#v when 0 then 'zero' when @#v2 then 'case 2' else 'something else' end +select @@c +set @@c = case @#v+@#v when 0 then 'zero' when @#v2 then 'case 2' else 'something else' end +select @@c +go +exec p22_atatuservar +go + +create trigger tr7_atatuservar on t7_trigger_atatuservar for insert +as +begin +select 'trigger tr7_atatuservar' + +declare @@z128_@@@$$@@@@$$@@@@$$@@@@$$@@@@$$@@@@$$@@@@$$@@@@$$@@@@$$@@@_____############################################################ varchar(30), @#v64_7890123456789$123456789$1234567890123456789012345678901234 int=1, @#w64_7890123456789$123456789$1234567890123456789012345678901234 int=2 +set @@z128_@@@$$@@@@$$@@@@$$@@@@$$@@@@$$@@@@$$@@@@$$@@@@$$@@@@$$@@@_____############################################################ = case when @#v64_7890123456789$123456789$1234567890123456789012345678901234 > 0 then '> zero' else '<- zero' end +select @@z128_@@@$$@@@@$$@@@@$$@@@@$$@@@@$$@@@@$$@@@@$$@@@@$$@@@@$$@@@_____############################################################ +set @@z128_@@@$$@@@@$$@@@@$$@@@@$$@@@@$$@@@@$$@@@@$$@@@@$$@@@@$$@@@_____############################################################ = case @#v64_7890123456789$123456789$1234567890123456789012345678901234 when 0 then 'zero' else '<> zero' end +select @@z128_@@@$$@@@@$$@@@@$$@@@@$$@@@@$$@@@@$$@@@@$$@@@@$$@@@@$$@@@_____############################################################ +set @@z128_@@@$$@@@@$$@@@@$$@@@@$$@@@@$$@@@@$$@@@@$$@@@@$$@@@@$$@@@_____############################################################ = case @#v64_7890123456789$123456789$1234567890123456789012345678901234 when 0 then 'zero' when @#w64_7890123456789$123456789$1234567890123456789012345678901234 then 'case 2' else 'something else' end +select @@z128_@@@$$@@@@$$@@@@$$@@@@$$@@@@$$@@@@$$@@@@$$@@@@$$@@@@$$@@@_____############################################################ +set @@z128_@@@$$@@@@$$@@@@$$@@@@$$@@@@$$@@@@$$@@@@$$@@@@$$@@@@$$@@@_____############################################################ = case @#v64_7890123456789$123456789$1234567890123456789012345678901234+@#v64_7890123456789$123456789$1234567890123456789012345678901234 when 0 then 'zero' when @#w64_7890123456789$123456789$1234567890123456789012345678901234 then 'case 2' else 'something else' end + +declare @#x64_7890123456789$123456789$1234567890123456789012345678901234 int = 3 +set @#x64_7890123456789$123456789$1234567890123456789012345678901234 *= -1 +set @#x64_7890123456789$123456789$1234567890123456789012345678901234 = @#x64_7890123456789$123456789$1234567890123456789012345678901234 + 10*@#x64_7890123456789$123456789$1234567890123456789012345678901234 +select @#x64_7890123456789$123456789$1234567890123456789012345678901234 = @#x64_7890123456789$123456789$1234567890123456789012345678901234 + 1 +if @#x64_7890123456789$123456789$1234567890123456789012345678901234 > 0 select 'positive' else select 'negative' +select @#x64_7890123456789$123456789$1234567890123456789012345678901234, @#x64_7890123456789$123456789$1234567890123456789012345678901234, '@#x64_7890123456789$123456789$1234567890123456789012345678901234' +print @#x64_7890123456789$123456789$1234567890123456789012345678901234 -- PRINT output not captured by JDBC tests + +declare @@v128_@@@$$@@@@$$@########@@@$$@@@@$$@@@@$$@@@###__##@@@$$@@@@$$@@@@$$@@@@$$@@@______@@@$$@@@@$$@@@@$$@@@###########@@@$$@@@@@@ int = 3 +set @@v128_@@@$$@@@@$$@########@@@$$@@@@$$@@@@$$@@@###__##@@@$$@@@@$$@@@@$$@@@@$$@@@______@@@$$@@@@$$@@@@$$@@@###########@@@$$@@@@@@ *= -1 +set @@v128_@@@$$@@@@$$@########@@@$$@@@@$$@@@@$$@@@###__##@@@$$@@@@$$@@@@$$@@@@$$@@@______@@@$$@@@@$$@@@@$$@@@###########@@@$$@@@@@@ = @@v128_@@@$$@@@@$$@########@@@$$@@@@$$@@@@$$@@@###__##@@@$$@@@@$$@@@@$$@@@@$$@@@______@@@$$@@@@$$@@@@$$@@@###########@@@$$@@@@@@ + 10*@@v128_@@@$$@@@@$$@########@@@$$@@@@$$@@@@$$@@@###__##@@@$$@@@@$$@@@@$$@@@@$$@@@______@@@$$@@@@$$@@@@$$@@@###########@@@$$@@@@@@ +select @@v128_@@@$$@@@@$$@########@@@$$@@@@$$@@@@$$@@@###__##@@@$$@@@@$$@@@@$$@@@@$$@@@______@@@$$@@@@$$@@@@$$@@@###########@@@$$@@@@@@ = @@v128_@@@$$@@@@$$@########@@@$$@@@@$$@@@@$$@@@###__##@@@$$@@@@$$@@@@$$@@@@$$@@@______@@@$$@@@@$$@@@@$$@@@###########@@@$$@@@@@@ + 1 +if @@v128_@@@$$@@@@$$@########@@@$$@@@@$$@@@@$$@@@###__##@@@$$@@@@$$@@@@$$@@@@$$@@@______@@@$$@@@@$$@@@@$$@@@###########@@@$$@@@@@@ > 0 select 'positive' else select 'negative' +select @@v128_@@@$$@@@@$$@########@@@$$@@@@$$@@@@$$@@@###__##@@@$$@@@@$$@@@@$$@@@@$$@@@______@@@$$@@@@$$@@@@$$@@@###########@@@$$@@@@@@ , @@v128_@@@$$@@@@$$@########@@@$$@@@@$$@@@@$$@@@###__##@@@$$@@@@$$@@@@$$@@@@$$@@@______@@@$$@@@@$$@@@@$$@@@###########@@@$$@@@@@@, '@@v128_@@@$$@@@@$$@########@@@$$@@@@$$@@@@$$@@@###__##@@@$$@@@@$$@@@@$$@@@@$$@@@______@@@$$@@@@$$@@@@$$@@@###########@@@$$@@@@@@' +print @@v128_@@@$$@@@@$$@########@@@$$@@@@$$@@@@$$@@@###__##@@@$$@@@@$$@@@@$$@@@@$$@@@______@@@$$@@@@$$@@@@$$@@@###########@@@$$@@@@@@ -- PRINT output not captured by JDBC tests + +declare @@y128_############################################______@@@$$@@@@$$@@@@__###############@@@$$@@@@$$@@@@$$@@@@$$@@@@$$@@@@$$@@@@ varchar(20)='abcd', @#p64_7890123456789$123456789$1234567890123456789012345678901234 int=2, @@#q64_890123456789$123456789$1234567890123456789012345678901234 int=1 +select substring(@@y128_############################################______@@@$$@@@@$$@@@@__###############@@@$$@@@@$$@@@@$$@@@@$$@@@@$$@@@@$$@@@@, @#p64_7890123456789$123456789$1234567890123456789012345678901234, @@#q64_890123456789$123456789$1234567890123456789012345678901234) + +declare @@q64_7890123456789$123456789$1234567890123456789012345678901234 varchar(20)='1', @#q4_7890123456789$123456789$1234567890123456789012345678901234 varchar(20)='3' +select * from t1_atatuservar where c like '%'+@@q64_7890123456789$123456789$1234567890123456789012345678901234 or c like '%'+@#q4_7890123456789$123456789$1234567890123456789012345678901234 order by 1 +end +go +insert t7_trigger_atatuservar values (123) +go + +-- table variables, in T-SQL batch, procedure and trigger (functions are below) +declare @@v table (a int) +insert @@v values (123) +select * from @@v +go + +declare @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 table (a int) +insert @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 values (123) +select * from @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 +go + +declare @#v table (a int) +insert @#v values (123) +insert @#v select * from @#v +select * from @#v +update @#v set a = a + 1 +select * from @#v +set rowcount 1 +delete @#v +set rowcount 0 +select * from @#v +go + +declare @@V table (a int) +insert @@v values (123) +insert @@v select * from @@v +select * from @@v +update @@v set a = a + 1 +select * from @@v +set rowcount 1 +delete @@v +set rowcount 0 +select * from @@v +go + +create procedure p12_atatuservar +as +declare @#v table (a int) +insert @#v values (123) +insert @#v select * from @#v +select * from @#v +update @#v set a = a + 1 +select * from @#v +set rowcount 1 +delete @#v +set rowcount 0 +select * from @#v + +declare @@V table (a int) +insert @@v values (123) +insert @@v select * from @@v +select * from @@v +update @@v set a = a + 1 +select * from @@v +set rowcount 1 +delete @@v +set rowcount 0 +select * from @@v +go +exec p12_atatuservar +go + +create procedure p13_atatuservar +as +declare @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 table (a int) +insert @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 values (123) +select * from @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 +insert @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 select * from @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 +select * from @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 +update @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 set a = a + 1 +select * from @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 +set rowcount 1 +delete @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 +set rowcount 0 +select * from @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 +go +exec p13_atatuservar +go + +create trigger tr8_atatuservar on t8_trigger_atatuservar for insert +as +begin +select 'trigger tr8_atatuservar' + +declare @#v table (a int) +insert @#v values (123) +insert @#v select * from @#v +select * from @#v +update @#v set a = a + 1 +select * from @#v +set rowcount 1 +delete @#v +set rowcount 0 +select * from @#v + +declare @@V table (a int) +insert @@v values (123) +insert @@v select * from @@v +select * from @@v +update @@v set a = a + 1 +select * from @@v +set rowcount 1 +delete @@v +set rowcount 0 +select * from @@v +end +go +insert t8_trigger_atatuservar values (123) +go + +create trigger tr9_atatuservar on t9_trigger_atatuservar for insert +as +begin +select 'trigger tr9_atatuservar' + +declare @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 table (a int) +insert @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 values (123) +select * from @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 +insert @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 select * from @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 +select * from @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 +update @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 set a = a + 1 +select * from @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 +set rowcount 1 +delete @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 +set rowcount 0 +select * from @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 +end +go +insert t9_trigger_atatuservar values (123) +go + +-- @@rowcount +declare @@V int = 1 +set rowcount @@v +select * from t1_atatuservar order by 1 +set @@v = 0 +set rowcount @@v +select * from t1_atatuservar order by 1 +go + +declare @#V int = 1 +set rowcount @#v +select * from t1_atatuservar order by 1 +set @#v = 0 +set rowcount @#v +select * from t1_atatuservar order by 1 +go + +create procedure p14_atatuservar +as +declare @@V int = 1 +set rowcount @@v +select * from t1_atatuservar order by 1 +set @@v = 0 +set rowcount @@v +select * from t1_atatuservar order by 1 + +declare @#V int = 1 +set rowcount @#v +select * from t1_atatuservar order by 1 +set @#v = 0 +set rowcount @#v +select * from t1_atatuservar order by 1 +go +exec p14_atatuservar +go + +create trigger tr10_atatuservar on t10_trigger_atatuservar for insert +as +begin +select 'trigger tr10_atatuservar' + +declare @@V int = 1 +set rowcount @@v +select * from t1_atatuservar order by 1 +set @@v = 0 +set rowcount @@v +select * from t1_atatuservar order by 1 + +declare @#V int = 1 +set rowcount @#v +select * from t1_atatuservar order by 1 +set @#v = 0 +set rowcount @#v +select * from t1_atatuservar order by 1 +end +go +insert t10_trigger_atatuservar values (123) +go + +create function f1_atatuservar_extra_long_name_here() +returns @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 table (a int) +as +begin +insert @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 +values (123) +return +end +go +select * from f1_atatuservar_extra_long_name_here() +go + +create function f2_atatuservar_extra_long_name_here() +returns @#v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 table (a int) +as +begin +insert @#v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 +values (123) +return +end +go +select * from f2_atatuservar_extra_long_name_here() +go + +-- in SET statements that support variable argument, procedure and trigger (functions are below) +declare @@v int = 4 +set datefirst @@v +select datepart(dw, '2024-10-21') +go +set datefirst 7 +go + +declare @#v int = 5 +set datefirst @#v +select datepart(dw, '2024-10-21') +go +set datefirst 7 +go + +declare @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 int = 5 +set datefirst @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 +select datepart(dw, '2024-10-21') +go +set datefirst 7 +go + +-- SET DATEFORMAT currently ignores the argument and will always use 'mdy' +declare @@v varchar(10) = 'dmy' +set dateformat @@v +select cast('01/02/03' as datetime) +go +set dateformat mdy +go + +-- SET DATEFORMAT currently silently ignores the argument and will always use 'mdy' +declare @#v varchar(10) = 'dmy' +set dateformat @#v +select cast('01/02/03' as datetime) +go +set dateformat mdy +go + +-- SET DATEFORMAT currently silently ignores the argument and will always use 'mdy' +declare @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 varchar(10) = 'dmy' +set dateformat @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 +select cast('01/02/03' as datetime) +go +set dateformat mdy +go + +-- SET LANGUAGE currently only supports english +declare @@v varchar(10) = 'dutch' +set language @@v +select @@language +go +set language us_english +go +declare @#v varchar(10) = 'french' +set language @#v +select @@language +go +set language us_english +go +declare @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 varchar(10) = 'french' +set language @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 +select @@language +go +set language us_english +go + +create procedure p15_atatuservar +as +declare @@v1 int = 4 +set datefirst @@v1 +select datepart(dw, '2024-10-21') + +declare @#v1 int = 5 +set datefirst @#v1 +select datepart(dw, '2024-10-21') + +declare @@v2 varchar(10) = 'dmy' +set dateformat @@v2 +select cast('01/02/03' as datetime) + +declare @#v3 varchar(10) = 'dutch' +set language @#v3 +select @@language +go + +exec p15_atatuservar +go +set datefirst 7 +set dateformat mdy +set language us_english +go +create procedure p16_atatuservar +as +declare @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 int = 4 +set datefirst @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 +select datepart(dw, '2024-10-21') + +declare @#v128_90123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 int = 5 +set datefirst @#v128_90123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 +select datepart(dw, '2024-10-21') + +declare @@w128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 varchar(10) = 'dmy' +set dateformat @@w128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 +select cast('01/02/03' as datetime) + +declare @#w128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 varchar(10) = 'dutch' +set language @#w128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 +select @@language +go +exec p15_atatuservar +go +set datefirst 7 +set dateformat mdy +set language us_english +go + +create trigger tr11_atatuservar on t11_trigger_atatuservar for insert +as +begin +select 'trigger tr11_atatuservar' + +declare @@v1 int = 4 +set datefirst @@v1 +select datepart(dw, '2024-10-21') + +declare @#v1 int = 5 +set datefirst @#v1 +select datepart(dw, '2024-10-21') + +declare @@v2 varchar(10) = 'dmy' +set dateformat @@v2 +select cast('01/02/03' as datetime) + +declare @#v3 varchar(10) = 'dutch' +set language @#v3 +select @@language +end +go +insert t11_trigger_atatuservar values (123) +go +set datefirst 7 +set dateformat mdy +set language us_english +go + +create trigger tr12_atatuservar on t12_trigger_atatuservar for insert +as +begin +select 'trigger tr12_atatuservar' + +declare @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 int = 4 +set datefirst @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 +select datepart(dw, '2024-10-21') + +declare @#v128_90123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 int = 5 +set datefirst @#v128_90123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 +select datepart(dw, '2024-10-21') + +declare @@w128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 varchar(10) = 'dmy' +set dateformat @@w128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 +select cast('01/02/03' as datetime) + +declare @#w128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 varchar(10) = 'dutch' +set language @#w128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 +select @@language +end +go +insert t12_trigger_atatuservar values (123) +go +set datefirst 7 +set dateformat mdy +set language us_english +go + +-- execute-immediate, procedure and trigger +declare @@V varchar(50) = 'select 123' execute(@@v) +go + +declare @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 varchar(50) = 'select 123' execute(@@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678) +go + +declare @@V varchar(50) = 'declare @#v int =123 select @#v' +execute(@@v) +go + +declare @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 varchar(500) = 'declare @#w128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 int =123 select @#w128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678' +execute(@@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678) +go + +declare @@V varchar(50) = 'declare @#v int =123 select @#v' +execute('select 456 ' + @@v) +go + +declare @#V varchar(50) = 'declare @@v int =123 select @@v' +execute(@#v) +go + +declare @#V varchar(50) = 'declare @@v int =123 select @@v' +execute('select 456 ' + @#v) +go + +declare @#V varchar(50) = 'select 123 ' +execute(@#v + @#v) +go + +declare @#V varchar(50) = 'select 123 ', @@V varchar(50) = 'select 456 ' +execute(@#v + @@v + @@v + @#v) +go + +declare @@V varchar(50) = 'select "abc''def" ' +execute(@@v + 'select "xy''z" ' + @@V) +go + +create procedure p17_atatuservar +as +declare @#V1 varchar(50) = 'declare @@v int =123 select @@v' +execute('select 456 ' + @#v1) + +declare @#V2 varchar(50) = 'select 123 ' +execute(@#v2 + @#v2) + +declare @#V3 varchar(50) = 'select 123 ', @@V varchar(50) = 'select 456 ' +execute(@#v3 + @@v + @@v + @#v3) + +declare @@V4 varchar(50) = 'select "abc''def" ' +execute(@@v4 + 'select "xy''z" ' + @@V4) +go +exec p17_atatuservar +go + +create trigger tr13_atatuservar on t13_trigger_atatuservar for insert +as +begin +select 'trigger tr13_atatuservar' + +declare @#V1 varchar(50) = 'declare @@v int =123 select @@v' +execute('select 456 ' + @#v1) + +declare @#V2 varchar(50) = 'select 123 ' +execute(@#v2 + @#v2) + +declare @#V3 varchar(50) = 'select 123 ', @@V varchar(50) = 'select 456 ' +execute(@#v3 + @@v + @@v + @#v3) + +declare @@V4 varchar(50) = 'select "abc''def" ' +execute(@@v4 + 'select "xy''z" ' + @@V4) +end +go +insert t13_trigger_atatuservar values (123) +go + +create procedure p18_atatuservar +as +begin +declare @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 varchar(500) = 'declare @#w128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 int =123 select @#w128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678' +execute(@@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678) +end +go +exec p18_atatuservar +go + +create trigger tr14_atatuservar on t14_trigger_atatuservar for insert +as +begin +select 'trigger tr14_atatuservar' + +declare @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 varchar(500) = 'declare @#w128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 int =123 select @#w128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678' +execute(@@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678) +end +go +insert t14_trigger_atatuservar values (123) +go + +-- RAISERROR, procedure and trigger +declare @@v int=50001 +raiserror(@@v,1,1) +go + +declare @@v int=50001, @@v2 int=1 +raiserror(@@v,@@v2,1) +go + +declare @@v int=50001, @@v2 int=1, @#v3 int=1 +raiserror(@@v,@@v2,@#v3) +go + +declare @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 int=50001, @#w128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 int=1, @@y128_############################################______@@@$$@@@@$$@@@@__###############@@@$$@@@@$$@@@@$$@@@@$$@@@@$$@@@@$$@@@@ int=1 +raiserror(@@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678, @#w128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678, @@y128_############################################______@@@$$@@@@$$@@@@__###############@@@$$@@@@$$@@@@$$@@@@$$@@@@$$@@@@$$@@@@) +go + +create procedure p1_raiserror_atatuservar +as +declare @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 int=50001, @#w128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 int=1, @@y128_############################################______@@@$$@@@@$$@@@@__###############@@@$$@@@@$$@@@@$$@@@@$$@@@@$$@@@@$$@@@@ int=1 +raiserror(@@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678, @#w128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678, @@y128_############################################______@@@$$@@@@$$@@@@__###############@@@$$@@@@$$@@@@$$@@@@$$@@@@$$@@@@$$@@@@) +go +exec p1_raiserror_atatuservar +go + +create trigger tr15_atatuservar on t15_trigger_atatuservar for insert +as +begin +declare @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 int=50001, @#w128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 int=1, @@y128_############################################______@@@$$@@@@$$@@@@__###############@@@$$@@@@$$@@@@$$@@@@$$@@@@$$@@@@$$@@@@ int=1 +raiserror(@@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678, @#w128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678, @@y128_############################################______@@@$$@@@@$$@@@@__###############@@@$$@@@@$$@@@@$$@@@@$$@@@@$$@@@@$$@@@@) +end +go +insert t15_trigger_atatuservar values (123) +go + + +-- sp_executesql with named/unnamed arguments, output parameters, in T-SQL batch, procedure, trigger +exec sp_executesql N'declare @@v int=123 select @@v' +go + +exec sp_executesql N'SELECT @@v1, @#v2', N'@@v1 int, @#v2 varchar(20)', @#v2='Hello, World!', @@v1=123 +go + +declare @@sql nvarchar(50)=N'SELECT @@v1, @#v2' +exec sp_executesql @@sql, N'@@v1 int, @#v2 varchar(20)', @#v2='Hello, World!', @@v1=123 +go + +declare @@params nvarchar(50)=N'@@v1 int, @#v2 varchar(20)' +exec sp_executesql N'SELECT @@v1, @#v2', @@params, @#v2='Hello, World!', @@v1=123 +go + +declare @@sql nvarchar(50)=N'SELECT @@v1, @#v2' +declare @@params nvarchar(50)=N'@@v1 int, @#v2 varchar(20)' +exec sp_executesql @@sql, @@params, @#v2='Hello, World!', @@v1=123 +go + +declare @@sql nvarchar(50)=N'SELECT @@v1, @#v2', @@params nvarchar(50)=N'@@v1 int, @#v2 varchar(20)', @@v3 int=123, @#v4 varchar(20)='Hello, World!' +exec sp_executesql @@sql, @@params, @#v2=@#v4, @@v1=@@v3 +go + +declare @@SQLString1 NVARCHAR(100); +declare @#SQLString2 NVARCHAR(max); +declare @@ParamDef NVARCHAR(100); +SET @@SQLString1 = N'SET @#a = @@b + @@b'; +SET @#SQLString2 = N'EXEC sp_executesql N''SET @#a = @@b + @@b'', N''@#a INT OUT, @@b INT'', @#a OUTPUT, @@b;'; +SET @@ParamDef = N'@#a INT OUTPUT, @@b INT'; +declare @@p INT; +declare @#a INT; +EXEC sp_executesql @@SQLString1, @@ParamDef, @#a = @@p OUT, @@b = 10; +EXEC sp_executesql @#SQLString2, @@ParamDef, @#a = @#a OUT, @@b = 11; +SELECT @@p, @#a; +go + +create procedure p1_spexec_atatuservar +as +declare @@sql nvarchar(50)=N'SELECT @@v1, @#v2', @@params nvarchar(50)=N'@@v1 int, @#v2 varchar(20)', @@v3 int=123, @#v4 varchar(20)='Hello, World!' +exec sp_executesql @@sql, @@params, @#v2=@#v4, @@v1=@@v3 +declare @@SQLString1 NVARCHAR(100) +declare @#SQLString2 NVARCHAR(max) +declare @@ParamDef NVARCHAR(100) +SET @@SQLString1 = N'SET @#a = @@b + @@b' +SET @#SQLString2 = N'EXEC sp_executesql N''SET @#a = @@b + @@b'', N''@#a INT OUT, @@b INT'', @#a OUT, @@b' +SET @@ParamDef = N'@#a INT OUTPUT, @@b INT' +declare @@p INT +declare @#a INT +EXEC sp_executesql @@SQLString1, @@ParamDef, @#a = @@p OUT, @@b = 10 +EXEC sp_executesql @#SQLString2, @@ParamDef, @#a = @#a OUT, @@b = 11 +SELECT @@p, @#a +go +exec p1_spexec_atatuservar +go + +create trigger tr16_atatuservar on t16_trigger_atatuservar for insert +as +begin +select 'trigger tr16_atatuservar' + +declare @@sql nvarchar(50)=N'SELECT @@v1, @#v2', @@params nvarchar(50)=N'@@v1 int, @#v2 varchar(20)', @@v3 int=123, @#v4 varchar(20)='Hello, World!' +exec sp_executesql @@sql, @@params, @#v2=@#v4, @@v1=@@v3 +declare @@SQLString1 NVARCHAR(100) +declare @#SQLString2 NVARCHAR(max) +declare @@ParamDef NVARCHAR(100) +SET @@SQLString1 = N'SET @#a = @@b + @@b' +SET @#SQLString2 = N'EXEC sp_executesql N''SET @#a = @@b + @@b'', N''@#a INT OUT, @@b INT'', @#a OUT, @@b' +SET @@ParamDef = N'@#a INT OUTPUT, @@b INT' +declare @@p INT +declare @#a INT +EXEC sp_executesql @@SQLString1, @@ParamDef, @#a = @@p OUT, @@b = 10 +EXEC sp_executesql @#SQLString2, @@ParamDef, @#a = @#a OUT, @@b = 11 +SELECT @@p, @#a +end +go +insert t16_trigger_atatuservar values (123) +go + +declare @@v63_7890123456789$123456789$123456789012345678901234567890123 NVARCHAR(1000); +declare @#w63_7890123456789$123456789$123456789012345678901234567890123 NVARCHAR(max); +declare @@x63_7890123456789$123456789$123456789012345678901234567890123 NVARCHAR(1000); +SET @@v63_7890123456789$123456789$123456789012345678901234567890123 = N'SET @@z63_7890123456789$123456789$123456789012345678901234567890123 = @@p63_7890123456789$123456789$123456789012345678901234567890123 + @@p63_7890123456789$123456789$123456789012345678901234567890123'; +SET @#w63_7890123456789$123456789$123456789012345678901234567890123 = N'EXEC sp_executesql N''SET @@z63_7890123456789$123456789$123456789012345678901234567890123 = @@p63_7890123456789$123456789$123456789012345678901234567890123 + @@p63_7890123456789$123456789$123456789012345678901234567890123'', N''@@z63_7890123456789$123456789$123456789012345678901234567890123 INT OUT, @@p63_7890123456789$123456789$123456789012345678901234567890123 INT'', @@z63_7890123456789$123456789$123456789012345678901234567890123 OUTPUT, @@p63_7890123456789$123456789$123456789012345678901234567890123;'; +SET @@x63_7890123456789$123456789$123456789012345678901234567890123 = N'@@z63_7890123456789$123456789$123456789012345678901234567890123 INT OUTPUT, @@p63_7890123456789$123456789$123456789012345678901234567890123 INT'; +declare @#y63_7890123456789$123456789$123456789012345678901234567890123 INT; +declare @@z63_7890123456789$123456789$123456789012345678901234567890123 INT; +EXEC sp_executesql @@v63_7890123456789$123456789$123456789012345678901234567890123, @@x63_7890123456789$123456789$123456789012345678901234567890123, @@z63_7890123456789$123456789$123456789012345678901234567890123 = @#y63_7890123456789$123456789$123456789012345678901234567890123 OUT, @@p63_7890123456789$123456789$123456789012345678901234567890123 = 10; +EXEC sp_executesql @#w63_7890123456789$123456789$123456789012345678901234567890123, @@x63_7890123456789$123456789$123456789012345678901234567890123, @@z63_7890123456789$123456789$123456789012345678901234567890123 = @@z63_7890123456789$123456789$123456789012345678901234567890123 OUT, @@p63_7890123456789$123456789$123456789012345678901234567890123 = 11; +SELECT @#y63_7890123456789$123456789$123456789012345678901234567890123, @@z63_7890123456789$123456789$123456789012345678901234567890123; +go + +create procedure p2_spexec_atatuservar +as +declare @@v63_7890123456789$123456789$123456789012345678901234567890123 NVARCHAR(1000); +declare @#w63_7890123456789$123456789$123456789012345678901234567890123 NVARCHAR(max); +declare @@x63_7890123456789$123456789$123456789012345678901234567890123 NVARCHAR(1000); +SET @@v63_7890123456789$123456789$123456789012345678901234567890123 = N'SET @@z63_7890123456789$123456789$123456789012345678901234567890123 = @@p63_7890123456789$123456789$123456789012345678901234567890123 + @@p63_7890123456789$123456789$123456789012345678901234567890123'; +SET @#w63_7890123456789$123456789$123456789012345678901234567890123 = N'EXEC sp_executesql N''SET @@z63_7890123456789$123456789$123456789012345678901234567890123 = @@p63_7890123456789$123456789$123456789012345678901234567890123 + @@p63_7890123456789$123456789$123456789012345678901234567890123'', N''@@z63_7890123456789$123456789$123456789012345678901234567890123 INT OUT, @@p63_7890123456789$123456789$123456789012345678901234567890123 INT'', @@z63_7890123456789$123456789$123456789012345678901234567890123 OUTPUT, @@p63_7890123456789$123456789$123456789012345678901234567890123;'; +SET @@x63_7890123456789$123456789$123456789012345678901234567890123 = N'@@z63_7890123456789$123456789$123456789012345678901234567890123 INT OUTPUT, @@p63_7890123456789$123456789$123456789012345678901234567890123 INT'; +declare @#y63_7890123456789$123456789$123456789012345678901234567890123 INT; +declare @@z63_7890123456789$123456789$123456789012345678901234567890123 INT; +EXEC sp_executesql @@v63_7890123456789$123456789$123456789012345678901234567890123, @@x63_7890123456789$123456789$123456789012345678901234567890123, @@z63_7890123456789$123456789$123456789012345678901234567890123 = @#y63_7890123456789$123456789$123456789012345678901234567890123 OUT, @@p63_7890123456789$123456789$123456789012345678901234567890123 = 10; +EXEC sp_executesql @#w63_7890123456789$123456789$123456789012345678901234567890123, @@x63_7890123456789$123456789$123456789012345678901234567890123, @@z63_7890123456789$123456789$123456789012345678901234567890123 = @@z63_7890123456789$123456789$123456789012345678901234567890123 OUT, @@p63_7890123456789$123456789$123456789012345678901234567890123 = 11; +SELECT @#y63_7890123456789$123456789$123456789012345678901234567890123, @@z63_7890123456789$123456789$123456789012345678901234567890123; +go +exec p2_spexec_atatuservar +go + + +create trigger tr17_atatuservar on t17_trigger_atatuservar for insert +as +begin +select 'trigger tr17_atatuservar' + +declare @@v63_7890123456789$123456789$123456789012345678901234567890123 NVARCHAR(1000); +declare @#w63_7890123456789$123456789$123456789012345678901234567890123 NVARCHAR(max); +declare @@x63_7890123456789$123456789$123456789012345678901234567890123 NVARCHAR(1000); +SET @@v63_7890123456789$123456789$123456789012345678901234567890123 = N'SET @@z63_7890123456789$123456789$123456789012345678901234567890123 = @@p63_7890123456789$123456789$123456789012345678901234567890123 + @@p63_7890123456789$123456789$123456789012345678901234567890123'; +SET @#w63_7890123456789$123456789$123456789012345678901234567890123 = N'EXEC sp_executesql N''SET @@z63_7890123456789$123456789$123456789012345678901234567890123 = @@p63_7890123456789$123456789$123456789012345678901234567890123 + @@p63_7890123456789$123456789$123456789012345678901234567890123'', N''@@z63_7890123456789$123456789$123456789012345678901234567890123 INT OUT, @@p63_7890123456789$123456789$123456789012345678901234567890123 INT'', @@z63_7890123456789$123456789$123456789012345678901234567890123 OUTPUT, @@p63_7890123456789$123456789$123456789012345678901234567890123;'; +SET @@x63_7890123456789$123456789$123456789012345678901234567890123 = N'@@z63_7890123456789$123456789$123456789012345678901234567890123 INT OUTPUT, @@p63_7890123456789$123456789$123456789012345678901234567890123 INT'; +declare @#y63_7890123456789$123456789$123456789012345678901234567890123 INT; +declare @@z63_7890123456789$123456789$123456789012345678901234567890123 INT; +EXEC sp_executesql @@v63_7890123456789$123456789$123456789012345678901234567890123, @@x63_7890123456789$123456789$123456789012345678901234567890123, @@z63_7890123456789$123456789$123456789012345678901234567890123 = @#y63_7890123456789$123456789$123456789012345678901234567890123 OUT, @@p63_7890123456789$123456789$123456789012345678901234567890123 = 10; +EXEC sp_executesql @#w63_7890123456789$123456789$123456789012345678901234567890123, @@x63_7890123456789$123456789$123456789012345678901234567890123, @@z63_7890123456789$123456789$123456789012345678901234567890123 = @@z63_7890123456789$123456789$123456789012345678901234567890123 OUT, @@p63_7890123456789$123456789$123456789012345678901234567890123 = 11; +SELECT @#y63_7890123456789$123456789$123456789012345678901234567890123, @@z63_7890123456789$123456789$123456789012345678901234567890123; +end +go +insert t17_trigger_atatuservar values (123) +go + +-- table variables in different types of functions +create function f1_ins_atatuservar(@@p1 int) +returns int +as +begin + declare @@tv table(a int) + insert @@tv values(@@p1) + return (select sum(a) from @@tv) +end +go +select dbo. f1_ins_atatuservar(123) +go + +create function f1_upd_atatuservar(@@p1 int) +returns int +as +begin + declare @@tv table(a int) + insert @@tv values(@@p1) + update @@tv set a = a + 1 + return (select sum(a) from @@tv) +end +go +select dbo. f1_upd_atatuservar(123) +go + +create function f1_del_atatuservar(@@p1 int) +returns int +as +begin + declare @@tv table(a int) + insert @@tv values(@@p1) + update @@tv set a = a + 1 + insert @@tv values(@@p1) + update @@tv set a = a + 1 + delete @@tv where a = 124 + return (select sum(a) from @@tv) +end +go +select dbo. f1_del_atatuservar(123) +go + +-- INSERT/UPDATE/DELETE on table variable in function +create function f3_atatuservar_upd(@@p1 int) +returns @@tv table(a int) +as +begin + insert @@tv values(@@p1) + update t set a = t.a + 1 from @@tv as t + return +end +go +select * from dbo. f3_atatuservar_upd(123) order by 1 +go + +create function f3_atatuservar_del(@@p1 int) +returns @@tv table(a int) +as +begin + insert @@tv values(@@p1) + update @@tv set a = a + 1 + insert @@tv values(@@p1) + delete t from @@tv as t where a = 124 + return +end +go +select * from dbo. f3_atatuservar_del(123) order by 1 +go + +create function f3_upd_atatuservar(@@p1 int) +returns @@tv table(a int) +as +begin + insert @@tv values(@@p1) + update t set a = t.a + 1 from t2_atatuservar, @@tv as t where t2_atatuservar.a = t.a + return +end +go +select * from dbo. f3_upd_atatuservar(123) order by 1 +go + +create function f3_del_atatuservar(@@p1 int) +returns @@tv table(a int) +as +begin + insert @@tv values(@@p1) + update @@tv set a = a + 1 + insert @@tv values(@@p1) + delete t from t2_atatuservar join @@tv as t on t2_atatuservar.a = t.a + return +end +go +select * from dbo. f3_del_atatuservar(123) order by 1 +go + +create function f4_ins_atatuservar(@#p1 int) +returns int +as +begin + declare @#tv table(a int) + insert @#tv values(@#p1) + return (select sum(a) from @#tv) +end +go +select dbo. f4_ins_atatuservar(123) +go + +create function f4_upd_atatuservar(@#p1 int) +returns int +as +begin + declare @#tv table(a int) + insert @#tv values(@#p1) + update @#tv set a = a + 1 + return (select sum(a) from @#tv) +end +go +select dbo. f4_upd_atatuservar(123) +go + +create function f4_del_atatuservar(@#p1 int) +returns int +as +begin + declare @#tv table(a int) + insert @#tv values(@#p1) + update @#tv set a = a + 1 + insert @#tv values(@#p1) + update @#tv set a = a + 1 + delete @#tv where a = 124 + return (select sum(a) from @#tv) +end +go +select dbo. f4_del_atatuservar(123) +go + +create function f5_upd_atatuservar(@#p1 int) +returns @#tv table(a int) +as +begin + insert @#tv values(@#p1) + update t set a = t.a + 1 from @#tv as t + return +end +go +select * from dbo. f5_upd_atatuservar(123) order by 1 +go + +create function f5_del_atatuservar(@#p1 int) +returns @#tv table(a int) +as +begin + insert @#tv values(@#p1) + update @#tv set a = a + 1 + insert @#tv values(@#p1) + delete t from @#tv as t where a = 124 + return +end +go +select * from dbo. f5_del_atatuservar(123) order by 1 +go + +create function f6_upd_atatuservar(@#p1 int) +returns @#tv table(a int) +as +begin + insert @#tv values(@#p1) + update t set a = t.a + 1 from t2_atatuservar, @#tv as t where t2_atatuservar.a = t.a + return +end +go +select * from dbo. f6_upd_atatuservar(123) order by 1 +go + +create function f6_del_atatuservar(@#p1 int) +returns @#tv table(a int) +as +begin + insert @#tv values(@#p1) + update @#tv set a = a + 1 + insert @#tv values(@#p1) + delete t from t2_atatuservar, @#tv as t where t2_atatuservar.a = t.a + return +end +go +select * from dbo. f6_del_atatuservar(123) order by 1 +go + +create function f7_tabvar_udf_upd_atatuservar(@@p1 int) +returns @#tv table(a int) +as +begin + insert @#tv values(@@p1) + update t set a = t.a + 1 from t2_atatuservar, @#tv as t where t2_atatuservar.a = t.a + return +end +go +select * from dbo. f7_tabvar_udf_upd_atatuservar(123) order by 1 +go + +create function f7a_tabvar_udf_upd_atatuservar(@@p1 int) +returns @#tv table(a int) +as +begin + insert @#tv values(@@p1) + update t set a = t.a + 1 from @#tv as tv, t2_atatuservar as t where tv.a = t.a + return +end +go + +create function f8_tabvar_udf_del_atatuservar(@@p1 int) +returns @#tv table(a int) +as +begin + insert @#tv values(@@p1) + update @#tv set a = a + 1 + insert @#tv values(@@p1) + delete t from @#tv as tv, t2_atatuservar as t where tv.a = t.a + return +end +go + +create function f9_tabvar_udf_del_atatuservar(@p1 int) +returns @tv table(a int) +as +begin + insert @tv values(@p1) + update @tv set a = a + 1 + insert @tv values(@p1) + delete t from @tv as t inner join t2_atatuservar as tv on tv.a = t.a + return +end +go +select * from dbo.f9_tabvar_udf_del_atatuservar(123) +go + +create function f10_tabvar_udf_del_atatuservar(@#p1 int) +returns @@tv table(a int) +as +begin + insert @@tv values(@#p1) + update @@tv set a = a + 1 + insert @@tv values(@#p1) + delete t from t2_atatuservar as tv join @@tv as t on tv.a = t.a + return +end +go +select * from dbo.f10_tabvar_udf_del_atatuservar(123) +go + +create function f4_atatuservar_tabvar_in_function_upd(@#p1 int) +returns @@tv table(a int) +as +begin + insert @@tv values(@#p1) + update t set a = t.a + 1 from t2_atatuservar as t2_atatuservar left join t2_atatuservar as tv on tv.a = t2_atatuservar.a right join @@tv t on tv.a = t.a + return +end +go +select * from dbo.f4_atatuservar_tabvar_in_function_upd(123) +go + +create function f12_tabvar_udf_upd_atatuservar(@#p1 int) +returns @@tv table(a int) +as +begin + insert @@tv values(@#p1) + update t set a = t.a + 1 from @@tv as tv cross join t2_atatuservar as t + return +end +go + +create function f13_tabvar_udf_del_atatuservar(@#p1 int) +returns @@tv table(a int) +as +begin + insert @@tv values(@#p1) + update @@tv set a = a + 1 + insert @@tv values(@#p1) + delete t from @@tv as t2_atatuservar right join t2_atatuservar as tv on tv.a = t2_atatuservar.a full outer join t2_atatuservar t on tv.a = t.a + return +end +go + +create function f14_del_atatuservar(@@p128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 int) +returns int +as +begin + declare @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 table(a int) + insert @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 values(@@p128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678) + update @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 set a = a + 1 + insert @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 values(@@p128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678) + update @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 set a = a + 1 + delete @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 where a = 124 + return (select sum(a) from @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678) +end +go +select dbo. f14_del_atatuservar(123) +go + +-- INSERT/UPDATE/DELETE on table variable in function +create function f15_upd_atatuservar(@@p128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 int) +returns @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 table(a int) +as +begin + insert @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 values(@@p128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678) + update t set a = t.a + 1 from @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 as t + return +end +go +select * from dbo. f15_upd_atatuservar(123) order by 1 +go + +create function f16_tabvar_udf_del_atatuservar(@#q128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 int) +returns @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 table(a int) +as +begin + insert @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 values(@#q128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678) + update @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 set a = a + 1 + insert @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 values(@#q128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678) + delete t from @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 as t2_atatuservar right join t2_atatuservar as tv on tv.a = t2_atatuservar.a full outer join t2_atatuservar t on tv.a = t.a + return +end +go + +-- THROW +declare @@v int = 50001; +THROW @@v, 'Test message', 1; +go + +declare @@v int = 50001, @@v2 varchar(30) = 'Test message'; +THROW @@v, @@v2, 1 +go + +declare @@v int = 50001, @@v2 varchar(30) = 'Test message', @@v3 int=1; +THROW @@v, @@v2, @@v3 +go + +declare @#v int = 50001; +THROW @#v, 'Test message', 1; +go + +declare @#v int = 50001, @#v2 varchar(30) = 'Test message'; +THROW @#v, @#v2, 1 +go + +declare @#v int = 50001, @#v2 varchar(30) = 'Test message', @#v3 int=1; +THROW @#v, @#v2, @#v3 +go + +create procedure p20_atatuservar @@p int +as +if @@p > 0 +begin +declare @@v int = 50001, @@v2 varchar(30) = 'Test message 1', @@v3 int=1; +THROW @@v, @@v2, @@v3 +end + +declare @#v int = 50001, @#v2 varchar(30) = 'Test message 2', @#v3 int=1; +THROW @#v, @#v2, @#v3 +go +exec p20_atatuservar 1 +go +exec p20_atatuservar 0 +go + +create trigger tr18_atatuservar on t18_trigger_atatuservar for insert +as +begin +select 'trigger tr18_atatuservar' + +declare @#v int = 50001, @#v2 varchar(30) = 'Test message 2', @#v3 int=1; +THROW @#v, @#v2, @#v3 +end +go +insert t18_trigger_atatuservar values (123) +go + +declare @#v128_90123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 int = 50001, @#v128_90123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$123456789012345678901234567890123456782 varchar(30) = 'Test message', @#v128_90123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$123456789012345678901234567890123456783 int=1; +THROW @#v128_90123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678, @#v128_90123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$123456789012345678901234567890123456782, @#v128_90123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$123456789012345678901234567890123456783 +go + +create procedure p21_atatuservar +as +declare @#v128_90123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 int = 50001, @#v128_90123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$123456789012345678901234567890123456782 varchar(30) = 'Test message', @#v128_90123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$123456789012345678901234567890123456783 int=1; +THROW @#v128_90123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678, @#v128_90123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$123456789012345678901234567890123456782, @#v128_90123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$123456789012345678901234567890123456783 +go +exec p21_atatuservar +go + +create trigger tr19_atatuservar on t19_trigger_atatuservar for insert +as +begin +select 'trigger tr19_atatuservar' + +declare @#v128_90123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 int = 50001, @#v128_90123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$123456789012345678901234567890123456782 varchar(30) = 'Test message', @#v128_90123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$123456789012345678901234567890123456783 int=1; +THROW @#v128_90123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678, @#v128_90123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$123456789012345678901234567890123456782, @#v128_90123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$123456789012345678901234567890123456783 +end +go +insert t19_trigger_atatuservar values (123) +go + +-- DBCC CHECKIDENT +declare @@TableName varchar(100)='mytab' +DBCC CHECKIDENT (@@TableName) +go +declare @@v int=2 +DBCC CHECKIDENT ('mytab', RESEED, @@v) +go + +create procedure p1_checkident_atatuservar @@TableName varchar(100), @@v int +as +DBCC CHECKIDENT (@@TableName, RESEED, @@v) +go + +create trigger tr20_atatuservar on t20_trigger_atatuservar for insert +as +begin +select 'trigger tr20_atatuservar' +declare @@TableName varchar(100)='mytab', @@v int=2 +DBCC CHECKIDENT (@@TableName, RESEED, @@v) +end +go + +declare @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 int=2 +DBCC CHECKIDENT ('mytab', RESEED, @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678) +go + +create procedure p2_checkident_atatuservar @@t128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 varchar(100), @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 int +as +DBCC CHECKIDENT (@@t128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678, RESEED, @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678) +go + +create trigger tr21_atatuservar on t21_trigger_atatuservar for insert +as +begin +select 'trigger tr21_atatuservar' +declare @@t128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 varchar(100)='mytab', @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 int=2 +DBCC CHECKIDENT (@@t128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678, RESEED, @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678) +end +go + +-- transaction name +declare @@V varchar(50) = 'myxact' +begin transaction @@v +insert t3_atatuservar values(234) +declare @#V varchar(50) = 'mysavept' +save transaction @#v +insert t3_atatuservar values(345) +set @#V = 'mysavept' +rollback transaction @#v +insert t3_atatuservar values(567) +commit transaction @@v +go +select * from t3_atatuservar +go + +create procedure p1_xactname_atatuservar +as +declare @@V varchar(50) = 'myxact' +delete t3_atatuservar where a > 123 +begin transaction @@v +insert t3_atatuservar values(234) +declare @#V varchar(50) = 'mysavept' +save transaction @#v +insert t3_atatuservar values(345) +set @#V = 'mysavept' +rollback transaction @#v +insert t3_atatuservar values(567) +commit transaction @@v +select * from t3_atatuservar +go +exec p1_xactname_atatuservar +go + +delete t3_atatuservar where a > 123 +declare @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 varchar(50) = 'myxact' +begin transaction @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 +insert t3_atatuservar values(234) +declare @#v128_90123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 varchar(50) = 'mysavept' +save transaction @#v128_90123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 +insert t3_atatuservar values(345) +set @#v128_90123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 = 'mysavept' +rollback transaction @#v128_90123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 +insert t3_atatuservar values(567) +commit transaction @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 +go +select * from t3_atatuservar +go + +create procedure p2_xactname_atatuservar +as +declare @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 varchar(50) = 'myxact' +delete t3_atatuservar where a > 123 +begin transaction @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 +insert t3_atatuservar values(234) +declare @#v128_90123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 varchar(50) = 'mysavept' +save transaction @#v128_90123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 +insert t3_atatuservar values(345) +set @#v128_90123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 = 'mysavept' +rollback transaction @#v128_90123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 +insert t3_atatuservar values(567) +commit transaction @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 +select * from t3_atatuservar +go +exec p2_xactname_atatuservar +go + +-- cursor variable in T-SQL batch, stored procedure, trigger +declare @#v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 varchar(20) +declare @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 cursor +set @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 = cursor for select a from t_checkident_atatuservar order by n +open @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 +while 1=1 +begin + fetch from @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 into @#v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 + if @@fetch_status <> 0 break + select @#v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 +end +close @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 +deallocate @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 +go + +create procedure p1_cursor_atatuservar +as +begin + declare @#v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 varchar(20) + declare @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 cursor + set @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 = cursor for select a from t_checkident_atatuservar order by n + open @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 + while 1=1 + begin + fetch from @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 into @#v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 + if @@fetch_status <> 0 break + select @#v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 + end + close @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 + deallocate @@v128_890123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 +end +go +exec p1_cursor_atatuservar +go + +create trigger tr22_atatuservar on t22_trigger_atatuservar for insert +as +begin + select 'trigger tr22_atatuservar' + + declare @@v128_90123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 varchar(20) + declare @#v128_90123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 cursor + set @#v128_90123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 = cursor for select a from t_checkident_atatuservar order by n + open @#v128_90123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 + while 1=1 + begin + fetch from @#v128_90123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 into @@v128_90123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 + if @@fetch_status <> 0 break + select @@v128_90123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 + end + close @#v128_90123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 + deallocate @#v128_90123456789$123456789$1234567890123456789$123456789$1234567890123456789$123456789$12345678901234567890123456789012345678 +end +go +insert t22_trigger_atatuservar values (123) +go +set quoted_identifier on +go + +-- user-defined variables that were previously mapped to internal sys. functions, e.g. sys.rand() +declare @@xact_state int=123 select @@xact_state, xact_state() +go +declare @@error_line int=123 select @@error_line, error_line() +go +declare @@error_message int=123 select @@error_message, error_message() +go +declare @@error_number int=123 select @@error_number, error_number() +go +declare @@error_procedure int=123 select @@error_procedure, error_procedure() +go +declare @@error_severity int=123 select @@error_severity, error_severity() +go +declare @@error_state int=123 select @@error_state, error_state() +go +declare @@db_id int=123 select @@db_id, db_id('master') +go +declare @@db_name int=123 select @@db_name, db_name() +go +declare @@exp int=123 select @@exp, exp(1) +go +declare @@sign int=123 select @@sign, sign(1) +go +declare @@lock_timeout int=123 select @@lock_timeout, lock_timeout() +go +declare @@rand int=123 select @@rand, case when rand() <= 0 then 'too small, <= 0' when rand() >= 1 then ' too large, >= 1' else 'correct, >0 && <1' end +go +declare @@trigger_nestlevel int=123 select @@trigger_nestlevel, trigger_nestlevel() +go +declare @@atn2 int=123 select @@atn2, atn2(1,0) +go +declare @@datepart int=123 select @@datepart, datepart(yy, '2024-11-12') +go +declare @@datediff int=123 select @@datediff, datediff(dd, '2024-11-12', '2024-12-13') +go +declare @@datediff_big int=123 select @@datediff_big, datediff(dd, '2024-11-12', '2024-12-13') +go +declare @@dateadd int=123 select @@dateadd, datediff(dd, 3, '2024-11-12') +go +declare @@datename int=123 select @@datename, datename(dw, '2024-11-12') +go + +create proc p1_sysfunctions_atatuservar +as +begin +declare @@xact_state int=123 select @@xact_state, xact_state() +declare @@error_line int=123 select @@error_line, error_line() +declare @@error_message int=123 select @@error_message, error_message() +declare @@error_number int=123 select @@error_number, error_number() +declare @@error_procedure int=123 select @@error_procedure, error_procedure() +declare @@error_state int=123 select @@error_state, error_state() +declare @@db_id int=123 select @@db_id, db_id('master') +declare @@db_name int=123 select @@db_name, db_name() +declare @@exp int=123 select @@exp, exp(1) +declare @@sign int=123 select @@sign, sign(1) +declare @@lock_timeout int=123 select @@lock_timeout, lock_timeout() +declare @@rand int=123 select @@rand, case when rand() <= 0 then 'too small, <= 0' when rand() >= 1 then ' too large, >= 1' else 'correct, >0 && <1' end +declare @@trigger_nestlevel int=123 select @@trigger_nestlevel, trigger_nestlevel() +declare @@atn2 int=123 select @@atn2, atn2(1,0) +declare @@datepart int=123 select @@datepart, datepart(yy, '2024-11-12') +declare @@datediff int=123 select @@datediff, datediff(dd, '2024-11-12', '2024-12-13') +declare @@datediff_big int=123 select @@datediff_big, datediff(dd, '2024-11-12', '2024-12-13') +declare @@dateadd int=123 select @@dateadd, datediff(dd, 3, '2024-11-12') +declare @@datename int=123 select @@datename, datename(dw, '2024-11-12') +end +go +exec p1_sysfunctions_atatuservar +go + +create function f1_sysfunctions_atatuservar() returns int +as +begin +declare @@xact_state int=123 +declare @@error_line int=123 +declare @@error_message int=123 +declare @@error_number int=123 +declare @@error_procedure int=123 +declare @@error_state int=123 +declare @@db_id int=123 +declare @@db_name int=123 +declare @@exp int=123 +declare @@sign int=123 +declare @@lock_timeout int=123 +declare @@rand int=123 +declare @@trigger_nestlevel int=123 +declare @@atn2 int=123 +declare @@datepart int=123 +declare @@datediff int=123 +declare @@datediff_big int=123 +declare @@dateadd int=123 +declare @@datename int=123 +return @@xact_state + @@error_line + @@error_message + @@error_number + @@error_procedure + @@error_state + @@db_id + @@db_name + @@exp + @@sign + @@lock_timeout + @@rand + @@trigger_nestlevel + @@atn2 + @@datepart + @@datediff + @@datediff_big + @@dateadd + @@datename +end +go +select dbo.f1_sysfunctions_atatuservar() +go + +create trigger tr1_sysfunctions_atatuservar on t23_trigger_atatuservar for insert as +begin +declare @@xact_state int=123 select @@xact_state, xact_state() +declare @@error_line int=123 select @@error_line, error_line() +declare @@error_message int=123 select @@error_message, error_message() +declare @@error_number int=123 select @@error_number, error_number() +declare @@error_procedure int=123 select @@error_procedure, error_procedure() +declare @@error_state int=123 select @@error_state, error_state() +declare @@db_id int=123 select @@db_id, db_id('master') +declare @@db_name int=123 select @@db_name, db_name() +declare @@exp int=123 select @@exp, exp(1) +declare @@sign int=123 select @@sign, sign(1) +declare @@lock_timeout int=123 select @@lock_timeout, lock_timeout() +declare @@rand int=123 select @@rand, case when rand() <= 0 then 'too small, <= 0' when rand() >= 1 then ' too large, >= 1' else 'correct, >0 && <1' end +declare @@trigger_nestlevel int=123 select @@trigger_nestlevel, trigger_nestlevel() +declare @@atn2 int=123 select @@atn2, atn2(1,0) +declare @@datepart int=123 select @@datepart, datepart(yy, '2024-11-12') +declare @@datediff int=123 select @@datediff, datediff(dd, '2024-11-12', '2024-12-13') +declare @@datediff_big int=123 select @@datediff_big, datediff(dd, '2024-11-12', '2024-12-13') +declare @@dateadd int=123 select @@dateadd, datediff(dd, 3, '2024-11-12') +declare @@datename int=123 select @@datename, datename(dw, '2024-11-12') +end +go +insert t23_trigger_atatuservar values(1) +go diff --git a/test/JDBC/input/unquoted_string-vu-prepare.mix b/test/JDBC/input/unquoted_string-vu-prepare.mix index 62d7b5a6f8..3fe27c23b7 100644 --- a/test/JDBC/input/unquoted_string-vu-prepare.mix +++ b/test/JDBC/input/unquoted_string-vu-prepare.mix @@ -109,11 +109,6 @@ create procedure unqStr_proc_5 @p1 varchar(20), @p2 varchar(20) = @p1 as select @p2 go --- should raise error: -create procedure unqStr_proc_6 @p1 varchar(20) = @@myvar -as select @p1 -go - create procedure unqStr_proc_7 @p1 varchar(20) = N'aB"c''D', @p2 varchar(20) = dEfg, @p3 varchar(20) = "x'Y""z" as select @p1, @p2, @p3 go diff --git a/test/JDBC/input/unquoted_string-vu-verify.mix b/test/JDBC/input/unquoted_string-vu-verify.mix index 1f436118c3..cf5b7a68af 100644 --- a/test/JDBC/input/unquoted_string-vu-verify.mix +++ b/test/JDBC/input/unquoted_string-vu-verify.mix @@ -311,7 +311,10 @@ go -- should raise an error about column not found: CALL unqStr_pgproc_1(aBc); go - +-- should raise an error since variable is not defined +create procedure unqStr_proc_6 @p1 varchar(20) = @@myvar +as select @p1 +go -- should print 'aBc': select unqStr_pgfunc_1('aBc'); go diff --git a/test/JDBC/upgrade/latest/schedule b/test/JDBC/upgrade/latest/schedule index 4db9a875fc..ac99e617a5 100644 --- a/test/JDBC/upgrade/latest/schedule +++ b/test/JDBC/upgrade/latest/schedule @@ -498,6 +498,7 @@ alter_proc_recompile catalogs_dbo_sys_schema-upgrade unary_plus_op_string tabvar_in_function +atatuservar BABEL-4217 Test_ISNULL BABEL-4270 diff --git a/test/python/expected/sql_validation_framework/expected_create.out b/test/python/expected/sql_validation_framework/expected_create.out index 6abda76486..3f8259a134 100644 --- a/test/python/expected/sql_validation_framework/expected_create.out +++ b/test/python/expected/sql_validation_framework/expected_create.out @@ -116,9 +116,6 @@ Could not find upgrade tests for function sys.dateadd_internal Could not find upgrade tests for function sys.datediff_internal Could not find upgrade tests for function sys.datepart_internal Could not find upgrade tests for function sys.default_domain -Could not find upgrade tests for function sys.error_procedure -Could not find upgrade tests for function sys.exp -Could not find upgrade tests for function sys.fetch_status Could not find upgrade tests for function sys.floor Could not find upgrade tests for function sys.fn_helpcollations Could not find upgrade tests for function sys.formatmessage @@ -140,10 +137,8 @@ Could not find upgrade tests for function sys.openjson_with Could not find upgrade tests for function sys.openquery_internal Could not find upgrade tests for function sys.options Could not find upgrade tests for function sys.patindex_ai_collations -Could not find upgrade tests for function sys.pgerror Could not find upgrade tests for function sys.remove_accents_internal_using_cache Could not find upgrade tests for function sys.role_id -Could not find upgrade tests for function sys.servername Could not find upgrade tests for function sys.sp_columns_managed_internal Could not find upgrade tests for function sys.sp_datatype_info_helper Could not find upgrade tests for function sys.sp_describe_first_result_set_internal