Reputation: 179
I am looking for a way to store data from one table in 2 local variables, in one select statement
DECLARE @TITLE NVARCHAR()
DECLARE @DESCRIPTION NVARCHAR()
So the structure is something like this
experienceID (int) | title (string) | description (string) | userId
2 asd 1 4 dqd asdsad 1 5 09 2 7 456 2
What i want to do is check if the user has any experience and if so store it, also in same select i would like to check if the user has ANY description associated with ANY experience and also store it in a variable as well.
So the result will be:
@TITLE = any value from title column if userId = X has experience
@DESCRIPTION = will be first 1st value that is not empty string
EX: for userId = 1 there are experiences so there will be a data for both title and description
EX: for userId = 2 there are experiences but only title has data
EX: for userId = 3 there are no experiences so i don't have any data
For all the cases I need to store the results in the 2 local variables. I don't care if they have or not value.
Upvotes: 2
Views: 93
Reputation: 460028
SELECT @TITLE = MIN(NULLIF(TITLE,'')), @DESCRIPTION = MIN(NULLIF(DESCRIPTION,''))
FROM dbo.Experiences
WHERE UserId = @UserId
GROUP BY UserId
Upvotes: 4
Reputation: 17964
DECLARE @TITLE NVARCHAR(20)
DECLARE @DESCRIPTION NVARCHAR(20)
SELECT @TITLE = title, @DESCRIPTION = description
from mytable
where description is not null
SELECT @TITLE, @DESCRIPTION
Upvotes: 0
Reputation: 972
DECLARE @VAR
DECLARE @VAR2
SET @VAR = SELECT DESCRIPTION FROM SOMETHING WHERE DESCRIPTION NOT NULL AND DESCRIPTION NOT LIKE '' AND userid = x
SET @VAR2 = SELECT ExperienceId FROM SOMETHING where userid = x
If you have user table that links to experience table inner join them on experienceid
Tweak the query to your needs.
Upvotes: 0