SQL Server 2008 Table Variable Temp table

Source: Internet
Author: User

Recently made a report in which stored procedures, cursors, CTE expressions, temporal tables, and table variables are used in the report.

The problem comes when the cursor iterates through the data in the CTE, stores the corresponding data in the variable, inserts the variable into the table variable, and after the cursor ends, wants to filter the table variable based on the parameters of the stored procedure.

When a SQL statement is stitched with dynamic SQL, an error occurs. A table variable is not declared when prompted.

In fact, table variables cannot be used in dynamic SQL. What do we do? I'm inserting data from a table variable into a temporary table, and then stitching it up with a utility temporary table in dynamic SQL.

I don't know if we have any better ideas.

Note:

Temp table:

#tablename

Table variables

DECLARE @tablename TABLE (    .....)

In the SQL splicing, stitching conditions have a certain special wording

DECLARE @sql NVARCHAR (Max) DECLARE @cloum NVARCHAR () SET @sql = ' select * FROM table WHERE Condition column = ' + " '" + @cloum + " '"

SQL Server 2008 Table Variable temp table

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.