SQLite, unlike MS SQL, does not support native variable syntax. However, you can use in-memory temporary tables to simulate similar functionality.
First, create an in-memory temporary table named "_Variables" to store your variables:
BEGIN; PRAGMA temp_store = 2; /* 使用内存存储 */ CREATE TEMP TABLE _Variables(Name TEXT PRIMARY KEY, RealValue REAL, IntegerValue INTEGER, BlobValue BLOB, TextValue TEXT);
Declare a variable named "VariableName" by inserting the variable name into the "_Variables" table:
INSERT INTO _Variables (Name) VALUES ('VariableName');
Assign values to your variables. In this example, we will assign it an integer:
UPDATE _Variables SET IntegerValue = 42 WHERE Name = 'VariableName';
You can now use the value assigned to a variable in an INSERT operation. In the following expression, the variable "VariableName" is used in the WHERE clause:
INSERT INTO Table1 (Column1, Column2) SELECT Column1, Column2 FROM Table2 WHERE Column1 > (SELECT COALESCE(RealValue, IntegerValue, BlobValue, TextValue) FROM _Variables WHERE Name = 'VariableName' LIMIT 1);
When you are done, don’t forget to close the transaction:
DROP TABLE _Variables; COMMIT;
The above is the detailed content of How Can I Simulate Variables in SQLite Inserts?. For more information, please follow other related articles on the PHP Chinese website!