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.
Author |
Topic |
tkv
Starting Member
2 Posts |
Posted - 2010-02-16 : 03:32:16
|
i am getting ambiguous column name 'events' when executing my stored procedure.alter PROCEDURE [dbo].[spGetEventsByDate] -- Add the parameters for the stored procedure here@StartDateTme as DateTime,@EndDateTime as DateTimeASBEGIN -- SET NOCOUNT ON added to prevent extra result sets from -- interfering with SELECT statements. SET NOCOUNT ON; declare @TransactionsDate as Datetime declare @TransactionsTime as DateTime declare @SerialNo as bigint declare @TransactionType as int declare @CardNum as int declare @DevId as int declare @TimeAtt as int declare @Machine as Int--DECLARE @Events CURSORDECLARE CurEvents CURSOR FORselect EvenTime,eventime,SerialNum ,DevID,empid,Machine from eventswhere EVENTTYPE=0 and EVENTID=0and eventime>=@StartDateTme and eventime<=@EndDateTimeorder by eventimeOPEN CurEvents;FETCH NEXT FROM CurEvents;WHILE @@FETCH_STATUS = 0BEGINset @Devid=0; FETCH NEXT FROM CurEventsINTO @TransactionsDate,@TransactionsTime,@SerialNo,@DevId,@CardNum,@Machineif(@DevId=0) RETURN set @TimeAtt=0 select @TimeAtt= timeatt from reader where panelid=@machine and readerid=@devid if(@TimeAtt=0) return else if(@TimeAtt=1) set @TransactionType=7 else if(@TimeAtt=2) set @TransactionType=8 else return insert into DP_IBA_EVENTS([DATE],[TIME],[SerialNo],[TRANSTYPE],[PIN],[ADDRESS]) VALUES (@TransactionsDate,@TransactionsTime,@SerialNo,@TransactionType,@CardNum,@DevId)END;CLOSE CurEvents;DEALLOCATE CurEvents;ENDGO |
|
Kristen
Test
22859 Posts |
Posted - 2010-02-16 : 03:38:46
|
" i am getting ambiguous column name 'events' when executing my stored procedure."If you execute the SQL in QA what is the exact message you see?It is probably referencing a Trigger, or maybe a View, (as there is no "events" column in your SProc) |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-02-16 : 03:41:59
|
why do you need cursor for this?Isnt below enough?insert into DP_IBA_EVENTS([DATE],[TIME],[SerialNo],[TRANSTYPE],[PIN],[ADDRESS])select e.EvenTime,e.eventime,e.SerialNum,case when r.timeatt =1 then 7 when r.timeatt=2 then 8 end,e.empid,e.DevIDfrom events ejoin reader ron r.panelid=e.Machine and r.readerid=e.DevIDwhere e.EVENTTYPE=0 and e.EVENTID=0and e.eventime>=@StartDateTme and e.eventime<=@EndDateTimeand r.timeatt <>0order by e.eventime ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
|
|
Kristen
Test
22859 Posts |
Posted - 2010-02-16 : 04:09:19
|
Maybe becauseselect @TimeAtt= timeatt from reader where panelid=@machine and readerid=@devidcould return multiple rows and random-value is required? Personally I would have used TOP 1 and ORDER BY on that, or checked @@ROWCOUNT so that it was either repeatable or error-checked. |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2010-02-16 : 04:38:17
|
<<Personally I would have used TOP 1 and ORDER BY on that>>or MIN or MAX of thatMadhivananFailing to plan is Planning to fail |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-02-16 : 04:42:23
|
quote: Originally posted by Kristen Maybe becauseselect @TimeAtt= timeatt from reader where panelid=@machine and readerid=@devidcould return multiple rows and random-value is required? Personally I would have used TOP 1 and ORDER BY on that, or checked @@ROWCOUNT so that it was either repeatable or error-checked.
I see many people who tend to like cursors based solutions and on most occasions than not they seem to have started their career in oracle and then moved to MSSQL------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
|
|
Kristen
Test
22859 Posts |
Posted - 2010-02-16 : 05:53:47
|
Also they have been a programmer using a procedural language, so Cursors "feels right" - it takes a while to get ones head around Set-based processes - after procedural languages (well ... it did for me!) |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-02-16 : 06:09:58
|
quote: Originally posted by Kristen Also they have been a programmer using a procedural language, so Cursors "feels right" - it takes a while to get ones head around Set-based processes - after procedural languages (well ... it did for me!)
Yeah..thats true too------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2010-02-16 : 07:13:23
|
I remember my first steps in SQL...Without a clue what is possible in SQL I was going to code my own GROUP BY using a cursor because I came from COBOL... No, you're never too old to Yak'n'Roll if you're too young to die. |
|
|
Kristen
Test
22859 Posts |
Posted - 2010-02-16 : 07:49:11
|
We had a NEXT / PREVIOUS record button in our application when I moved from ISAM to SQL (SQL Server 6.5 )For a table with a multi-part key, to get the NEXT record it did:SELECT Col1, Col2, ...FROM MyTableWHERE KeyField1 > @KeyField1 OR (KeyField1 = @KeyField1 AND KeyField2 > @KeyField2) OR (KeyField1 = @KeyField1 AND KeyField2 = @KeyField2 AND KeyField3 > @KeyField3)ORDER BY KeyField1, KeyField2, KeyField3 PK / Clustered Index on KeyField1, KeyField2, KeyField3Th application read the first row from the resultset and then closed the resultset SQL Server was OK. Oracle took about 30 seconds to do this (I suppose we had about 50,000 rows in the table). Users hated it - "How come the old system was so fast?" No idea if Oracle had any sort of TOP / LIMIT syntax back then, SQL Server certainly didn't! (Actually it did, SET ROWCOUNT, but I didn't know about that then) we were using generic SQL for all brands of SQL backend ... |
|
|
|
|
|
|
|