简体   繁体   中英

Solutions for tracking how many times certain parameters are used in a stored procedure SQL Server 2008

In my database have tables with structures similar to:

create table ItemNameSearches
(ItemName varchar(50) not null,timesSearched int not null,
primary key(ItemName))
and

create table ItemList
(ItemName varchar(50),
primary key (ItemName))

My idea is to have people enter in through a webform a comma-separated list of values so that they can get information about certain items. The table ItemList contains information about the item for which they searched (although the table structure doesn't reflect that in this example). If, however, the item searched for doesn't appear in the ItemList table, I would like for that ItemName to be inserted into the ItemNameSearches so I could have a better idea of what people are searching for.

Scenario 1: an item is searched for the first time and a new row is inserted into the ItemNameSearches table. I'm pretty sure this is an area for triggers, but I'm not familiar with using them so I wrote the following stored procedure:

create proc spSearchItemName
 @itemName1 varchar(50)
,@itemName2 varchar(50) = null
,@itemName3 varchar(50) = null
,@itemName4 varchar(50) = null
as
begin
;with searchList
as
(select x.itemName
 from (values (@itemName1)
,(@itemName2)
,(@itemName3)
,(@itemName4)
 ) as x(itemName)
 where x.itemName is not null
 --these are optional parameters just to give the user more flexibility
 --on if they want to look at multiple items at once or not
)
insert into ItemNameSearches(itemName,timesSearched)
values
(
(select  sl.itemName
from searchList as sl
left outer join ItemList as il
on il.itemName=sl.itemName
where il.itemName is null
--this subquery finds the items searched for that are not present in the
--itemlist table and sets timesSearched =1 for each 
),1
)
end
This is well and good for the items that are searched for that do not appear in the ItemList table, but I would have to do something like the following procedure if they DID search for an item that was in the ItemList table

;with searchList 
as
(select x.itemName
 from (values ('item 1')
,('item 2')
,('item 3')
,('item 5')
 ) as x(itemName)
)

update ins
set  timesSearched = timesSearched +1
from ItemNameSearches as ins
where itemName in
(select itemName from searchList)

So this will add 1 to the number of times an item was searched for if it exists in the ItemList table. Can someone provide a neat manner of how to solve these two different situations? Is this something that is a good candidate for triggers?

Thanks to @Gordon Linoff for providing the direction of using the merge statement, it ended up working perfectly for what I wanted to do. I ended up using the following sproc and it works fine.

alter proc spSearchDrugName
 @drugName1 varchar(50)
,@drugName2 varchar(50) = null
,@drugName3 varchar(50) = null
,@drugName4 varchar(50) = null
,@drugName5 varchar(50) = null
as
begin
declare @searchList table(drugName varchar(50))
insert into @searchList 
values (@drugName1)
      ,(@drugName2)
      ,(@drugName3)
      ,(@drugName4)
      ,(@drugName5)

    merge DrugListSearches as d
    using(select drugName from @searchList where drugName is not null) as s
    on s.drugName = d.drugName
    when matched then 
        update set d.timesSearched = d.timesSearched + 1
    when not matched then 
    insert (drugname,timesSearched) values (s.drugName,1);



end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM