My SQL knowledge is pretty limited and this seems like something that should be easy, but I can't figure it out.
I have two queries that I run with the same where clause. Previously, I've pasted the number in the where clause twice. I thought there had to be an easier way so I found that you can do variables.
My problem is when declaring the variable in the first query, it forgets it and doesn't run it for the second query. The terminology may be off on a lot of what I've explain, but the simple query below should explain it better. The second query fails and says
must declare the scalar variable @p_repid
which I have declared in the first query.
DECLARE @p_repid int=3427115
select fldRBuddyId, count(fldRBuddyId) "Repeats"
from tblMsgsOnAir_Type8 typ8
where fldCBuddyId = @p_repid
group by fldRBuddyID
having count(fldRBuddyId) > 1
order by "Repeats" desc
go
select FLDREPID,moa.FLDTGBID,tt.FLDNAME, count(*) "Count"
from TBLMSGSONAIR_1 moa
join TBLTOWERS_1 tt on moa.FLDTGBID=tt.FLDTGBID
where fldrepid = @p_repid
group by FLDREPID,moa.FLDTGBID,tt.FLDNAME
order by "Count" desc
go
Thanks in advance for any help.