Single quotation marks in SQL statements, double quotation marks processing method _mssql

Source: Internet
Author: User

The following is a separate story, although the INSERT statement is the same, but the Select, Update, and DELETE statements are the same.
If the following forms are available:
Mytabe
Field 1 Username String type (name)
Field 2 Age numeral type (ages)
Field 3 Birthday Date type (birthday)
Field 4 Marry Boolean (whether married, married to True, not married to false)
Field 5 leixing String type (type)
1. Insert String type
If you want to insert a person named Zhang Hong, because it is a string, so insert the name on both sides of a single apostrophe,
such as: Strsql= "Insert into MyTable (username) VALUES (' Zhang Hong ')"
If the name is now a variable thename, then write
Strsql= "Insert into MyTable (username) VALUES (' & thename & ')"
Here insert INTO mytable (username) VALUES (' is the part before Zhang Hong, thename is a string variable, ') is the part behind Zhang Hong.
Replace the thename variable with Cheng Changgong, then connect the three segments with &, and it becomes strsql= "Insert into MyTable (username) VALUES (' Zhang Hong ')".
If you want to insert two fields, such as "Zhang", the type is "student"
Strsql= "Insert into MyTable (username,leixing) VALUES (' Zhang ', ' student ')"
If the name is now a variable thename, the type is also a variable thetype,
Write: strsql= "Insert into MyTable (username,leixing) VALUES (' & thename &" ', ' "& Thetype &") "
As with the first example, the thename and Thetype are replaced, and the connectors are concatenated into the same string as the above.
2. Inserting a numeric type
If inserting a record of age 12, pay attention to the number without a single apostrophe:
Strsql= Insert into mytable values (12) If the age is now a variable theage, then:
Strsql= Insert into mytable values ("& Theage &")
Here insert INTO mytable (age) VALUES (which is the previous section of 12, Theage is an aging variable,) is the back portion of 12.
Replace the theage with the & connector to connect the three parts to the same character as above.
3. Insert Date Type
Date type is similar to string type, but the apostrophe is replaced with the # number. (However, you can also use a single apostrophe in an Access database)
Strsql= "Insert into MyTable (birthday) VALUES (#1980 -10-1#)"
If replaced with a date variable thedate strsql= "Insert into MyTable (birthday) VALUES (#" & Thedate & "#)"
4. Insert Boolean
Boolean and numeric similar: only two values true and false,
such as: Strsql= "Insert into MyTable (marry) VALUES (True)"
If you change to a Boolean variable Themarry
Strsql= "Insert into MyTable (birthday) VALUES (" & themarry& ")"
5. Comprehensive example
Insert a record with a name of Zhang Hong, age 12
Strsql= "Insert into MyTable (username,age) VALUES (' Zhang Hong ', 12)"
Careful attention to the formula: because the name is a string, so Zhang Hong on both sides added a single apostrophe; age is a number, so there is no extra apostrophe.
If replaced with a string variable thename and a numeric variable theage, it becomes:
Strsql= "Insert into MyTable (username,age) VALUES (' & thename &" ', "& Theage &") "
Note the above, in short, replace the variable, and then connect to complete the same string as the top.
6. Little Tips
One of the students groped out a little trick to change the following sentence to a variable:
Strsql= "Insert into MyTable (username) VALUES (' Zhang Hong ')"
Step one: First erase Zhang, add two quotes to the original position
Strsql= "Insert into MyTable (username) VALUES ('" ")"
Step two: Add two connectors in the Middle &
Strsql= "Insert into MyTable (username) VALUES (' & & ')"
Step three: Write the variable between two connectors
Strsql= "Insert into MyTable (username) VALUES (' & thename & ')"-

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.