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
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Set Variable name = result

Author  Topic 

Shilpa22
Starting Member

37 Posts

Posted - 2009-05-18 : 03:26:55
Hi All,

I am new to Sql Sever. Pls help me out.
I am trying to create a sp
CREATE PROCEDURE proc_ins_TestDetails
(
@Title nvarchar(100),
@Base_Filter_Id char(3)
)
as
set @Base_Filter_Id = 'select distinct(Base_Filter_Id) from filtersmaster'.

I want 'select distinct(Base_Filter_Id) from filtersmaster' result to be set in @Base_Filter_Id.

How to do it?



Thanks in Advance
Shilpa

ergen
Starting Member

5 Posts

Posted - 2009-05-18 : 03:50:32
This one will work, but I'm not sure if you are aware of the fact that this query in procedure can return more that 1 record. In such situation you will get an error... look at http://www.jacek-szarapa.com/index.php?p=sql&d=3 that will help with the problem when more than one record is returned.


CREATE PROCEDURE [dbo].proc_ins_TestDetails
(
@Title nvarchar(100),
@Base_Filter_Id char(3) output
)
as
begin
select @Base_Filter_Id = distinct(Base_Filter_Id) from filtersmaster
end
go

declare @Base_Filter_Id1 char(3);
exec proc_ins_TestDetails @Base_Filter_Id=@Base_Filter_Id1, @Title=''
select @Base_Filter_Id1


----------------
Jacek Szarapa
http://www.jacek-szarapa.com
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2009-05-18 : 04:43:12
if you have multiple value for base_filter_id, maybe you can form a CSV (comma separated value)

declare @base_filter_id varchar(max)
select @base_filter_id = isnull(@base_filter_id + ',', '') + base_filter_id from filtersmaster group by base_filter_id

and then pass the CSV value into your stored procedure. Your stored procedure will need to modify to handle the CSV input value.
Make use of CSVTable or fnParseList found here
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=25830&SearchTerms=CSVTable
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=76033



KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

bklr
Master Smack Fu Yak Hacker

1693 Posts

Posted - 2009-05-18 : 04:52:01
quote:
Originally posted by ergen

This one will work, but I'm not sure if you are aware of the fact that this query in procedure can return more that 1 record. In such situation you will get an error... look at http://www.jacek-szarapa.com/index.php?p=sql&d=3 that will help with the problem when more than one record is returned.


CREATE PROCEDURE [dbo].proc_ins_TestDetails
(
@Title nvarchar(100),
@Base_Filter_Id char(3) output
)
as
begin
select @Base_Filter_Id = distinct(Base_Filter_Id) from filtersmaster
end
go

declare @Base_Filter_Id1 char(3);
exec proc_ins_TestDetails @Title='',@base_filter_id1 output
select @Base_Filter_Id1


----------------
Jacek Szarapa
http://www.jacek-szarapa.com

Go to Top of Page

Shilpa22
Starting Member

37 Posts

Posted - 2009-05-18 : 05:01:04
Thanks Folks,
It resolved my problem..Thanks a lot for u r help

Thanks in Advance
Shilpa
Go to Top of Page

ergen
Starting Member

5 Posts

Posted - 2009-05-18 : 05:27:48
quote:
Originally posted by khtan

if you have multiple value for base_filter_id, maybe you can form a CSV (comma separated value)

declare @base_filter_id varchar(max)
select @base_filter_id = isnull(@base_filter_id + ',', '') + base_filter_id from filtersmaster group by base_filter_id

and then pass the CSV value into your stored procedure. Your stored procedure will need to modify to handle the CSV input value.
Make use of CSVTable or fnParseList found here
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=25830&SearchTerms=CSVTable
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=76033



KH
[spoiler]Time is always against us[/spoiler]





another approach to CSVTable is here http://www.jacek-szarapa.com/index.php?p=sql&d=2. there is no need to use loop to solve the problem.

----------------
Jacek Szarapa
http://www.jacek-szarapa.com
Go to Top of Page
   

- Advertisement -