franmcod
franmcod

Reputation: 497

insert and read to declared table

I'm using SQL Server, it compiles. When I insert rows into my declared table, it jumps some instructions (I debugged it) and I don't understand why..

userMedStockQuantity is a float too.

declare @t table 
(
    qnty float,
    prevdate Date
)

insert into @t (qnty, prevdate) 
    select 
        userMedStockQuantity, userMedStockDate 
    from 
        UserMedStock 
    where 
        userMedStockMedId = @mId 
        and userMedStockUserId = @uId 
    order by
        userMedStockDate Desc

declare @aux int

select @aux = count(*) from @t

if(@aux == 0)
    set @medLeft = 0
else
begin
    --stuff
end

Instead of that @aux hammer, I've used @@ROWCOUNT = 0 but same result. Did I indent something wrong, or can't I insert into that table like that?

Thanks in advance

Upvotes: 0

Views: 75

Answers (1)

Alex Kudryashev
Alex Kudryashev

Reputation: 9460

declare @t table (
    qnty float,
    prevdate Date
)
--simplified UserMedStock table
declare @UserMedStock table(userMedStockQuantity int, userMedStockDate datetime)
--if you comment next row then print is 'yes', if uncomment 'no'
insert @UserMedStock values(1,'2016-06-06'),(2,'2016-06-04')
declare @aux int, @medLeft int
insert into @t (qnty, prevdate) 
select userMedStockQuantity, userMedStockDate from @UserMedStock 
--where userMedStockMedId = @mId AND userMedStockUserId = @uId ORDER BY userMedStockDate Desc
select @aux = count(*) from @t
if(@aux  = 0)-- == here was an error
begin
    set @medLeft = 0
print 'yes'
end
else
begin
--stuff
print 'no'
end

Upvotes: 1

Related Questions