Hi all, I apologize if this has been asked before but I couldn't find it.
What is the correct way to loop through tables in a SQL server where the criteria is based on a string variable? I don't think I'm getting the kind or number of quotation marks right.
For example, I have written
I've tried a number of combinations of quotation marks around the `x'. But either Stata recognizes it as a variable to loop over, and the code returns an error message that an exec is required (even though it's written on the next line), or the variable is not recognized and the code runs but retrieve a series of empty datasets.
What is the correct way to loop through tables in a SQL server where the criteria is based on a string variable? I don't think I'm getting the kind or number of quotation marks right.
For example, I have written
Code:
foreach x in "a" "b" "c" {local db "DRIVER={driverName};DATABASE=dbName;Trusted_Connection=Yes;SERVER=serverName;" local sql "SELECT * FROM tableName WHERE VAR = "'`x''";" odbc load, exec("`sql'") conn("`db'") clearsave data_`x'.dta,replace}
Comment