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 |
Force
Starting Member
4 Posts |
Posted - 2006-11-15 : 16:16:55
|
I am creating a temp table from a union of two tables in two different db's and then running a query against another table in another db. They are all on the same server. The following code runs like a rocket and returns the correct results when when run in the Query Analyzer:Select c1,c2 ,c3,c4 ,c5,c6 Into #MarkTest From db1.dbo.table1Where convert(char(12),startDateTime,101) = '11/14/2006'UNION ALLSelect c1,c2,c3,c4,c5,c6 From db2.dbo.table2Where convert(char(12),startDateTime,101) = '11/14/2006'GOSelect * From #MarkTest c Where convert(char(12),startDateTime,101) = '11/14/2006' And NOT EXISTS (Select 'x' From db3.dbo.c1 b Where c.c1 = b.c1 And convert(char(12),startDateTime,101) = '11/14/2006')GODrop Table #MarkTestWhen I try to create a stored procedure from this query I get the following error:Invalid Object Name #MarkTestIs this choking on the validation as when the system parses the query #markTest doesn't exist? How can I write this as a stored procedure?Thanks for any assistance. |
|
Kristen
Test
22859 Posts |
Posted - 2006-11-15 : 16:28:34
|
The "compiler" can get in a bit of twist. e.g.CREATE PROCEDURE fooASSELECT *INTO #TEMPFROM MyTable...DROP TABLE #TEMPSELECT *INTO #TEMPFROM MyOtherTable Simple answer is to pre-create the #TEMP table at the top of the SProc, rather than using SELECT ... INTO #TEMP FROMThis is also preferred because the SELECT ... INTO route will hold a Lock on Table Creating in TEMDB for the duration of the query, where pre-creating the table will only hold the lock for just the time to Create the table itself, and not be influenced by the time to run the query.(And if you pre-create the #TEMP table you can:a) Index/PK itb) Put COLLATE statements on any char/varchar/text columns - so you are not at the mercy of TEMPDB having a different collation to your database/underlying tableKristen |
 |
|
Force
Starting Member
4 Posts |
Posted - 2006-11-15 : 16:47:53
|
Kristen, I really appreciate the assist. What I don't see is how to load the temp table with the Union results without using the "Into #MarkTest. When you say pre-create the temp table do you mean like this?Create Table #MarkTest(collectionID varchar(16) ,donornum varchar(16) ,lastname varchar(30) ,firstname varchar(28) ,batch uniqueidentifier(16)) How would I then load the results of the union into the temp table? Again, I really appreciate the help.Mark |
 |
|
X002548
Not Just a Number
15586 Posts |
|
snSQL
Master Smack Fu Yak Hacker
1837 Posts |
Posted - 2006-11-15 : 17:11:07
|
You need to remove those GO statements when you create the proc and it should work just fine. |
 |
|
Force
Starting Member
4 Posts |
Posted - 2006-11-15 : 17:16:07
|
Insert Into #MarkTest(c1,c2,c3,c4,c5)Select c1,c2,c3,c4,c5 From db1.dbo.tbl1Where convert(char(12),startDateTime,101) = '11/14/2006' UNION ALLSelect c1,c2,c3,c4,c5 From db2.dbo.tbl2 Where convert(char(12),startDateTime,101) = '11/14/2006'GOWhen I try this it gives me the error "Invalid Object name '#MarkTest'Where do I go from here? Thanks.Mark |
 |
|
snSQL
Master Smack Fu Yak Hacker
1837 Posts |
Posted - 2006-11-15 : 17:18:16
|
You need to remove those GO statements when you create the proc and it should work just fine. |
 |
|
Force
Starting Member
4 Posts |
Posted - 2006-11-15 : 18:21:06
|
snSQL, You are right on the money. Awesome. Thanks.Mark |
 |
|
|
|
|
|
|