sql server - Can't I select from actual table and the declared table at the same time? SQL ERROR -
i have tables declared , did work.
my final state is..
declare @tmptblc table( rowindex int identity(1,1) not null, officer varchar(40), date varchar(20), total_served int, total_serving_time varchar(16), avg_serving_time varchar(16), log_in_time varchar(16), log_off_time varchar(16), served_by_hour int, total_tran_served int, total_tran_time varchar(16), avg_tran_time varchar(16), avg_tran_per_cus float);
i declared @tmptblc table
declare @dt datetime; set @dt=cast('01-01-1980' datetime); insert @tmptblc select loginname 'officer', convert(varchar(10), regdate, 105) 'date', count(distinct(queueno)) 'total_served', convert(varchar(6), sum(datediff(second,nexttime,endtime))/3600)+ ':' + right('0' + convert(varchar(2), (sum(datediff(second,nexttime,endtime)) % 3600) / 60), 2)+ ':' + right('0' + convert(varchar(2), sum(datediff(second,nexttime,endtime)) % 60), 2) 'total_serving_time', convert(varchar(6), avg(datediff(second,nexttime,endtime))/3600)+ ':' + right('0' + convert(varchar(2), (avg(datediff(second,nexttime,endtime)) % 3600) / 60), 2)+ ':' + right('0' + convert(varchar(2), avg(datediff(second,nexttime,endtime)) % 60), 2) 'avg_serving_time', convert(varchar(8), min(nexttime), 108) 'log_in_time', convert(varchar(8), max(nexttime), 108) 'log_off_time', isnull(nullif(3600/datediff(second, @dt, cast(('01-01-1980 '+ cast(convert(varchar(6), avg(datediff(second,nexttime,endtime))/3600)+ ':' + right('0' + convert(varchar(2), (avg(datediff(second,nexttime,endtime)) % 3600) / 60), 2)+ ':' + right('0' + convert(varchar(2), avg(datediff(second,nexttime,endtime)) % 60), 2) varchar(30))) datetime)),0),0) 'served_by_hour', sum(matter_served) 'total_tran_served', convert(varchar(6), sum(matter_time)/3600)+ ':' + right('0' + convert(varchar(2), (sum(matter_time) % 3600) / 60), 2)+ ':' + right('0' + convert(varchar(2), sum(matter_time) % 60), 2) 'total_tran_time', convert(varchar(6), avg(matter_time)/3600)+ ':' + right('0' + convert(varchar(2), (avg(matter_time) % 3600) / 60), 2)+ ':' + right('0' + convert(varchar(2), avg(matter_time) % 60), 2) 'avg_tran_time', round(sum(matter_served)/cast(count(distinct(queueno)) float),1) 'avg_tran_per_cus' @tmptblb group loginname, regdate
i insert @tmptblc.
select * @tmptblc
i can results @tmptblc.
but..
when this,
select logincode login, @tmptblc @tmptblc.rowindex=1 , login.contents=@tmptblc.officer
i got error.
msg 137, level 15, state 2, line 109 must declare scalar variable "@tmptblc".
so start doubting login
table actual table , @tmptblc
1 created query.
can't used together?
if that's case, wht's solution.
tkz.
i found out if
select logincode login, @tmptblc
it ok.. means..
@tmptblc.rowindex=1 , login.contents=@tmptblc.officer
is problem.
you need alias table variables in order refer them in other parts of query (such join condition). parser still doesn't seem understand @
prefix might refer table variable despite fact have existed since sql server 2000 (edit: but looks may not straightforward change make)!
select logincode login join @tmptblc [@tmptblc] on login.contents=[@tmptblc].officer [@tmptblc].rowindex=1
to answer question in comments updates (you need use if update requires join otherwise refer column names unqualified) example syntax is
declare @t1 table (c1 int,c2 int) declare @t2 table (c1 int,c2 int) insert @t1 values(1,null) insert @t2 values(1,105) update t1 set t1.c2 = t2.c2 @t1 t1 join @t2 t2 on t1.c1 = t2.c1 select * @t1
Comments
Post a Comment