This post has been de-listed
It is no longer included in search results and normal feeds (front page, hot posts, subreddit posts, etc). It remains visible only via the author's post history.
1
[SQL] Stuck on a Scalar Variable Issue
Author Summary
Daveaham_Lincoln is
in
SQL
Post Body
I'm getting a scalar variable error when trying to set a variable as part of an exec string. I've declared it outside of the string, but it doesn't seem to pick it up inside the string. I don't believe there's another way to do this because I HAVE to have the @table_focus variable set dynamically (this is part of a larger script). Does anybody know the right way to do this?
DECLARE @pk_column varchar(50)
--build pk exec
SELECT @exec_pk = 'SELECT @pk_column = column_name FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE
WHERE OBJECTPROPERTY(OBJECT_ID(constraint_name), ' '''IsPrimaryKey''' ') = 1
AND table_name = ' '''' @table_focus ''''
--
--Get PK Column
EXEC(@exec_pk)
--
OUT Msg 137, Level 15, State 1, Line 1 Must declare the scalar variable "@pk_column".
Author
Account Strength
100%
Account Age
13 years
Verified Email
Yes
Verified Flair
No
Total Karma
10,478
Link Karma
8,040
Comment Karma
2,438
Profile updated: 3 days ago
Posts updated: 7 months ago
Subreddit
Post Details
Location
We try to extract some basic information from the post title. This is not
always successful or accurate, please use your best judgement and compare
these values to the post title and body for confirmation.
- Posted
- 10 years ago
- Reddit URL
- View post on reddit.com
- External URL
- reddit.com/r/learnprogra...