Hi,

I have a variable @FranchiseId declared in my stored procedure

If I try the following select command, will it work????

SELECT @FranciseId=(select FranchiseName FROM franchise WHERE username=@username),username
FROM MyTable

I hope you Understood My problem!!

Recommended Answers

All 10 Replies

try the following

SELECT FranchiseName INTO @FranciseId FROM franchise WHERE username=@username

@debasisdas Thanks!!
But I need to fetch the value of "@franchiseId" variable from franchise Table and then display this variable's value along with other columns from the Mytable table.Moreover I have to use this value with joins once the value is fetched correctly.

should I Do it dis way:

SELECT FranciseId=(select FranchiseName FROM franchise WHERE username=@username)
INTO Mytable

I think this will insert the frachiseId column value into the Mytable and from their i can fetch the records..
Is it correct????

what is your table name ?

franchise
or
MyTable

franchise is the table from where i have to fetch franchise id
and Mytable is the table in which i have records based on that franchise id

try this

SELECT f.FranchiseName INTO @FranciseName 
FROM franchise f , mytable m 
WHERE f.username=@username
and f.franchiseid = m.franchiseid

@debasisdas Thanks :)

This query will work after some editing i.e:

SELECT f.FranchiseName INTO @FranciseName
FROM franchise f
WHERE f.username=@username


Now I need to insert this variable(FranciseName) value in another table which contains a column as franchiseID in the same query.
The table name is F1 and the column name is franchiseID

what will be the correct command to execute the above!!

Thanks in advance!!

@debasisdas: oops!! sorry!! I tried but the command did not work...

Is their any other way to store/select/fetch the value of one column from a table into a variable declared by us and then use that variable value in other queries????

Please give solution to this!!!!

try this sample SQL

INSERT INTO tbl_temp2 (fld_id)
  SELECT tbl_temp1.fld_order_id
  FROM tbl_temp1 WHERE tbl_temp1.fld_order_id > 100;

@debasisdas Thanks for your reply!!
Appreciate your help:)
I got my quesry solved!!
i needed to declare a table type variable and store the value into it!!

My Query was solved by the following commands:

Create Table F_ID(franchiseId varchar(20))

insert into F_ID
select Franchises
From franchise


Declare @tempF_ID Table(franchiseId varchar(20))

Declare @ID varchar(100)

set @Id='select * from F_ID'


insert into @tempF_ID
exec(@ID)

select @ID=franchiseId from @tempF_ID

print @ID

now the @ID variable stores the of franchise ID

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.