-1

I need to start out by apologizing for my ignorance on this, but I'm really supposed to be the network guy here. Anyway... I'm trying to query a couple of tables using inner joins to find who was the person assigned to an item at a specific time the item had a change.

I'd like to try use a declare statement to define a list of names and then let me query run through all the other variables and then select when the user equals one of the users I have listed in my declare.

I thought maybe a table? Here's what I have tried so far with disastrous results.

DECLARE @RG TABLE ( AssignedTo VARCHAR(20))   
SET @RG = ('FirstName1.LastName1', 'FirstName2.LastName2', 'FirstName3.LastName3');


DECLARE @RG TABLE (AssignedTo VARCHAR);
INSERT INTO @RG VALUES ('FirstName1.LastName1'), ('FirstName2.LastName2'), ('FirstName3.LastName3');

Can anyone help a noob out here?

Joachim Sauer
  • 505
  • 1
  • 8
  • 18
  • You don't SET the value of a table variable. You INSERT INTO it the same as any other table. Your second statement works fine if you add a length to the column "AssignedTo varchar(255)" – Tab Alleman Dec 19 '14 at 14:26

1 Answers1

0

If you're trying to find certain names from one table where they exist within another table, then try this:

SELECT yt.FirstName, yt.LastName
FROM Your_table yt
INNER JOIN Your_other_table yot on (yot.Firstname + ' ' + yot.LastName) = 
    (yt.FirstName + ' ' + yt.LastName)

If you're wanting to find specific names using a variable, then try this:

DECLARE
    @FullName VARCHAR(MAX) = 'Your Name'  --This is your variable
SELECT FirstName, LastName
FROM Your_table
WHERE (Firstname + ' ' + LastName) = @FullName

If you're wanting to find specific names using multiple variables, then try this:

DECLARE
    @FullName1 VARCHAR(MAX) = 'Your Name'
    @FullName2 VARCHAR(MAX) = 'Yourother Name'
SELECT FirstName, LastName
FROM Your_table
WHERE (Firstname + ' ' + LastName) IN (
    @FullName1,
    @FullName2
)
Zach Allen
  • 192
  • 1
  • 9
  • Unfortunately, the list of names do not appear in a table. They are usernames of the folks doing work. We just tag who that user was at the time of the change made. What I'm trying to do is find out who specifically did a certain action at a specific time in the life cycle of an item. – NetworkGuy Jan 23 '15 at 15:36