Ask: create command statements dynamically in SQL Server

Source: Internet
Author: User

Table Name:Peishain(Warehouse receiving ticket)

Table Structure: innumber char (8), indate datetime, intype char (12 ),Cangwei char (12 ),Pihao char (18), inweight decimal (9, 2), remark varchar (80)

The dynamic statements built in SQL Server are as follows (find the statements that meet the conditions based on the value of cangwei, one condition cannot run, if multiple conditions are more troublesome ):

Declare @ sqlstring varchar (200)
Declare @ cangwei varchar (12)

Set @ cangwei = 'b1'

Set @ sqlstring = 'select * From peishain where cangwei ='+ @ Cangwei

Exec (@ sqlstring)

Why is it always prompted:

Server: Message 207, level 16, status 3, Row 1
The column 'b1 'is invalid.

You may not be able to solve this problem. Please take a look at it. Please have a ready-made dynamic creation statement for comprehensive query conditions.ProgramCodeShare with me (INT, datetime, Char, and Other types are involved in the where condition). Thank you!

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.