| Author |
Topic |
|
adjones1980
Starting Member
36 Posts |
Posted - 2007-07-18 : 09:05:26
|
I am getting these errors in Analyzer...Server: Msg 107, Level 16, State 3, Line 14The column prefix 'tmp1' does not match with a table name or alias name used in the query.Server: Msg 107, Level 16, State 1, Line 31The column prefix 'tbl_tmp_CP' does not match with a table name or alias name used in the query. ... but I can't for the life of me spot the error. I fear I am too much of a newbie to see the problem.I have pasted the code below for you guys to look at. This is my first project using T-SQL and making procedures so I apologies for any poor coding.Thanks in advance--------------------------------------------------------------CREATE PROCEDURE procCommitCPsToDatabase AS/* Get the list of CPs that are to be uploaded */IF object_id('tmp1', 'U') IS NOT NULL BEGIN DROP TABLE tmp1 ENDBEGIN CREATE TABLE tmp1 ( CPID nvarchar(50), UploadID nvarchar(50) ) INSERT INTO tmp1 (CPID, UploadID) SELECT CPID, UploadID FROM CPsForCommittingToDatabase /* ###### Need to copy CP data to the deleted CP tables ###### */ INSERT INTO tbl_final_UploadFile ([UID], [UploadID], [ContractorID], [OriginalFileName], [StoredFileName], [Status], [UploadMessage], [UploadDate], [Reject]) SELECT [UID], [UploadID], [ContractorID], [OriginalFileName], [StoredFileName], [Status], [UploadMessage], [UploadDate], [Reject] FROM tbl_tmp_UploadFile WHERE EXISTS( SELECT * FROM tmp1 WHERE tbl_tmp_UploadFile.UploadID = tmp1.UploadID ) AND NOT EXISTS ( SELECT * FROM tbl_final_UploadFile WHERE tbl_tmp_UploadFile.UploadID = tmp1.UploadID ) /* CP */ INSERT INTO tbl_final_CP ([UID], [CPID], [UploadID], [CP], [Area], [Day], [dCount], [iFlw], [iValid], [ValidationMsg], [UserComments], [Video], [ExtEnum]) SELECT [UID],[CPID],[UploadID],[CP],[Area],[Day],[dCount],[iFlw],[iValid],[ValidationMsg],[UserComments], [Video], [ExtEnum] FROM tbl_tmp_CP WHERE EXISTS( SELECT * FROM tmp1 WHERE tbl_tmp_CP.CPID = tmp1.CPID ) /* CountData */ INSERT INTO tbl_final_CountData ([UID], [CountID], [CPID], [iDir], [Hour], [PC], [2WMV], [CAR], [BUS], [LGV], [HGVR2], [HGVR3], [HGVR4], [HGVA3], [HGVA5], [HGVA6], [HGV], [AMV]) SELECT [UID], [CountID], [CPID], [iDir], [Hour], [PC], [2WMV], [CAR], [BUS], [LGV], [HGVR2], [HGVR3], [HGVR4], [HGVA3], [HGVA5], [HGVA6], [HGV], [AMV] FROM tbl_tmp_CountData WHERE EXISTS( SELECT * FROM tmp1 WHERE tbl_tmp_CP.CPID = tmp1.CPID ) /* Now set the status of the CP copies still in tmp tables to 4 (Committed to Database) */ UPDATE tbl_tmp_CP SET [Completed] = 4 WHERE EXISTS( SELECT * FROM tmp1 WHERE tbl_tmp_CP.CPID = tmp1.CPID ) /* Delete the temporary table */ DROP TABLE tmp1ENDGO |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-07-18 : 09:15:10
|
Why creating a permanent table and then drop it at the end ? Might as well create a temp table !
CREATE PROCEDURE procCommitCPsToDatabase AS/* Get the list of CPs that are to be uploaded */IF object_id('tmp1', 'U') IS NOT NULL BEGIN DROP TABLE tmp1 ENDBEGIN CREATE TABLE #tmp1 ( CPID nvarchar(50), UploadID nvarchar(50) ) INSERT INTO #tmp1 (CPID, UploadID) SELECT CPID, UploadID FROM CPsForCommittingToDatabase /* ###### Need to copy CP data to the deleted CP tables ###### */ INSERT INTO tbl_final_UploadFile ([UID], [UploadID], [ContractorID], [OriginalFileName], [StoredFileName], [Status], [UploadMessage], [UploadDate], [Reject]) SELECT [UID], [UploadID], [ContractorID], [OriginalFileName], [StoredFileName], [Status], [UploadMessage], [UploadDate], [Reject] FROM tbl_tmp_UploadFile WHERE EXISTS ( SELECT * FROM #tmp1 WHERE tbl_tmp_UploadFile.UploadID = #tmp1.UploadID ) AND NOT EXISTS ( SELECT * FROM tbl_final_UploadFile t WHERE tbl_tmp_UploadFile.UploadID = t.UploadID ) /* CP */ INSERT INTO tbl_final_CP ([UID], [CPID], [UploadID], [CP], [Area], [DAY], [dCount], [iFlw], [iValid], [ValidationMsg], [UserComments], [Video], [ExtEnum]) SELECT [UID],[CPID],[UploadID],[CP],[Area],[DAY],[dCount],[iFlw],[iValid],[ValidationMsg],[UserComments], [Video], [ExtEnum] FROM tbl_tmp_CP WHERE EXISTS( SELECT * FROM #tmp1 WHERE tbl_tmp_CP.CPID = #tmp1.CPID ) /* CountData */ INSERT INTO tbl_final_CountData ([UID], [CountID], [CPID], [iDir], [Hour], [PC], [2WMV], [CAR], [BUS], [LGV], [HGVR2], [HGVR3], [HGVR4], [HGVA3], [HGVA5], [HGVA6], [HGV], [AMV]) SELECT [UID], [CountID], [CPID], [iDir], [Hour], [PC], [2WMV], [CAR], [BUS], [LGV], [HGVR2], [HGVR3], [HGVR4], [HGVA3], [HGVA5], [HGVA6], [HGV], [AMV] FROM tbl_tmp_CountData WHERE EXISTS( SELECT * FROM #tmp1 WHERE tbl_tmp_CountData.CPID = #tmp1.CPID ) /* Now SET the status of the CP copies still IN tmp tables to 4 (Committed to Database) */ UPDATE tbl_tmp_CP SET [Completed] = 4 WHERE EXISTS( SELECT * FROM #tmp1 WHERE tbl_tmp_CP.CPID = #tmp1.CPID ) /* DELETE the temporary TABLE */ DROP TABLE tmp1ENDGO KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
DonAtWork
Master Smack Fu Yak Hacker
2167 Posts |
Posted - 2007-07-18 : 09:18:36
|
Insead of making a "temporary" permanent table, try using a table variable.DECLARE @tmp1 table(CPID nvarchar(50), UploadID nvarchar(50)) [Signature]For fast help, follow this link:http://weblogs.sqlteam.com/brettk/archive/2005/05/25.aspxLearn SQLhttp://www.sql-tutorial.net/ http://www.firstsql.com/tutor.htm http://www.w3schools.com/sql/default.asp |
 |
|
|
adjones1980
Starting Member
36 Posts |
Posted - 2007-07-18 : 09:26:59
|
| OK so putting '#' in front of the table name creates it in memory only then? This would definitely save me managing the tables. |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-07-18 : 09:32:09
|
quote: Originally posted by adjones1980 OK so putting '#' in front of the table name creates it in memory only then? This would definitely save me managing the tables.
yes. table name prefix with # is temporary table KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
adjones1980
Starting Member
36 Posts |
Posted - 2007-07-18 : 09:38:17
|
| OK, I made the changes that you suggested and got this error message...[code]Server: Msg 2714, Level 16, State 6, Line 2There is already an object named '#tmp1' in the database.[/ code] |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-07-18 : 09:40:34
|
Did you just highlight and select the body of the stored procedure and execute or you are getting this error from executing the stored procedure ? KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
adjones1980
Starting Member
36 Posts |
Posted - 2007-07-18 : 10:18:23
|
| I copied the content of the procedure. I'll try exec the procedure. |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-07-18 : 10:37:03
|
quote: Originally posted by adjones1980 I copied the content of the procedure. I'll try exec the procedure.
If you did that you will notice that I did not drop the temp table. Temp table will auto drop when it is out of scope. KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
adjones1980
Starting Member
36 Posts |
Posted - 2007-07-18 : 10:38:27
|
| Thanks, the proc ran with no errors this time. However, it didn't do what I wanted!! D'oh!Thanks again |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-07-18 : 10:39:44
|
Ok. Then please state what you wanted the Sp to do ? Provide table DDL, sample data and required result KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
|
|
|