Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 local variables problem

Author  Topic 

argon007
Starting Member

38 Posts

Posted - 2008-06-12 : 14:27:53
quote:
list the first and the last name of the employee with a name of Thomas. Use local variables for the first name and the last name and the @@ROWCOUNT command.


SET NOCOUNT ON
DECLARE @variable varchar(50)
SET @variable = 'name'
SELECT @variable = fname + ' ' + lname
FROM employee
WHERE @variable = '%Thomas%'
IF @@ROWCOUNT > 0
PRINT 'Employee Name is ' + @variable
ELSE
PRINT 'Employee not found'


when i execute it, it said employee not found. what do i need to add or revise?

it should be:

quote:
Employee Name is Argon Thomas.


visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-06-12 : 14:30:43
If you want to use % for searching use LIKE. ANd if you use = for comparison dont use %

SET NOCOUNT ON
DECLARE @variable varchar(50)
SET @variable = 'name'
SELECT @variable = fname + ' ' + lname
FROM employee
WHERE @variable LIKE '%Thomas%'
IF @@ROWCOUNT > 0
PRINT 'Employee Name is ' + @variable
ELSE
PRINT 'Employee not found'
Go to Top of Page

chiragkhabaria
Master Smack Fu Yak Hacker

1907 Posts

Posted - 2008-06-12 : 14:32:57
[code]
SET NOCOUNT ON
DECLARE @Fname varchar(50),
@Lname varchar(50)

SET @FName = 'Thomas'

SELECT fname + ' ' + lname
FROM employee
WHERE FName Like '%' + @Fname + '%'

IF @@ROWCOUNT > 0
PRINT 'Employee Name is ' + @variable
ELSE
PRINT 'Employee not found'
[/code]

Chirag

http://www.chirikworld.com
Go to Top of Page

argon007
Starting Member

38 Posts

Posted - 2008-06-12 : 17:13:00
try it above, but it is the same problem. i don't know what it is wrong.

and Argon Thomas is on the employee table. i don't know why.
Go to Top of Page

chiragkhabaria
Master Smack Fu Yak Hacker

1907 Posts

Posted - 2008-06-12 : 17:31:56
[code]
SET NOCOUNT ON

DECLARE @variable varchar(50)



SELECT @variable = fname + ' ' + lname
FROM @employee
WHERE fname + ' ' + lname LIKE '%thomas%'

IF @@ROWCOUNT > 0
PRINT 'Employee Name is ' + @variable
ELSE
PRINT 'Employee not found'
[/code]

Chirag

http://www.chirikworld.com
Go to Top of Page

argon007
Starting Member

38 Posts

Posted - 2008-06-13 : 03:15:53
thanks, problem solved.
Go to Top of Page
   

- Advertisement -