Stored procedure problem

Hi All.
I try to create stored procedure in SQL Server. What is wrong in SELECT statement and to fix it?

Declare @SelectString as varchar(100)

    If @SelectText = 0 
    	Set @SelectString = 'Select' 
    Else
    	SELECT 
    		@SelectString = Last_Name+', '+First_Name EmployeeName
    	FROM  [dbo].[Employees]

Thanks

hi

if i put this code as it is

it does not know what @SelectText is
NOT DECLARED

what are you trying to do ? .... with @SelectString

please dont mind my asking
if i am clear about what you are trying to do
this is a very simple solution
:slight_smile: :slight_smile:

Hi harishgg1. Thanks for replay.

I created that SP to populate ComboBox. Just show how to concatenate columns in SELECT statement under IF statement for SP parameter? Why in my code in SELECT concatenation not works

Thanks.

Ok eugz

Will work on it

Please give me 1 hour or so

you cannot do this @SelectString = Last_Name+', '+First_Name EmployeeName
give an alias to the concatenation while also assigning it to a variable. it is one or the other

create table #Employees(First_Name varchar(50), Last_Name varchar(50))
insert into #Employees
select 'eu', 'gz' union
select 'harish', 'gg1' union
select 'yosias', 'z' 
go



create proc getemployees_sp
(@SelectText int)
as
begin

Declare @SelectString as varchar(100)

    If @SelectText = 0 
    	select 'Select' 
    Else
    	SELECT Last_Name +', '+ First_Name as EmployeeName
    	FROM  #Employees
end
go

exec getemployees_sp 3
go

drop table #Employees

hi eugz

i worked on it

one way of doing it

if it helps great
:slight_smile: :slight_smile:

drop create table ..
drop table #Employees
go 

create table #Employees(First_Name varchar(50), Last_Name varchar(50))
insert into #Employees
select 'eu', 'gz' union
select 'harish', 'gg1' union
select 'yosias', 'z' 
go

select * from #Employees
go
stored proc
CREATE PROCEDURE Getemployees_sp (@getresults INT) 
AS 
    SET nocount ON; 

    DECLARE @t TABLE 
      ( 
         first_name_last_name VARCHAR(50) 
      ) 

    IF @getresults = 0 
      INSERT @t 
      SELECT 'SELECT' 
    ELSE 
      INSERT @t 
      SELECT last_name + ', ' + first_name 
      FROM   #employees 

    SELECT * 
    FROM   @t 

go
stored proc execution results ...

+++++++++++++++++++++++++++++++++++++++++++
+++++++++++++++++++++++++++++++++++++++++++
image

++++++++++++++++++++++++++++++++++++++++++++++++++++
++++++++++++++++++++++++++++++++++++++++++++++++++++
image