2

I was wondering if these two queries could be combined into a single query?

Query 1: to get the @guidID to be plugged into Query 2:

DECLARE @guidID uniqueIdentifier
    SET @guidID = (SELECT guidID FROM dbo.table1 WHERE IntID = 1) 

Query 2: retrieves a combined table from a function and table1

SELECT o.guidID, IntID, Title, func.Name 
  FROM dbo.table1 o
 INNER JOIN dbo.func1(o.guidID) func ON func.guidID = o.guidID
 WHERE IntID = 1

func1 takes in a guidID object Returns a table of (guidID, IntID

Thanks for any help, this is as far as I've gotten, which give me the data that I need but in two separate queries.

Brett
  • 4,051
  • 2
  • 26
  • 39

1 Answers1

2

SQL Server 2005+ has CROSS APPLY

SELECT t.guidID, IntID, Title, func.Name 
  FROM
     dbo.table1 o
     CROSS APPLY
     dbo.func1(o.guidID) func
 WHERE IntID = 1

Edit: won't work... However, assuming you have one row for IntID = 1, you can also cross join for other DBMS/older SQL Servers

SELECT t.guidID, IntID, Title, func.Name 
  FROM
     dbo.table1 o
    CROSS JOIN
    dbo.func1(o.guidID) func
 WHERE IntID = 1

gbn
  • 422,506
  • 82
  • 585
  • 676
  • I'm getting an error: "Incorrect syntax near '.'." for the (o.guidID) on line 5. Any suggestions? I've been looking into it, but can't seem to solve the problem. – Brett Jun 29 '10 at 20:12
  • ah. You're on SQL Server 2000 or you have the db compat level at 80 (or lower). Use the 2nd form I gave please – gbn Jun 29 '10 at 20:23
  • I'm on SQL Server 2008, and I'm not sure what the db compat level is, as googling didn't tell me too much. But I get the same error when I attempt to run the second form. – Brett Jun 29 '10 at 21:27
  • http://stackoverflow.com/questions/1501596/how-to-check-sql-server-database-compatibility-after-sp-dbcmptlevel-is-deprecated and my 2nd form is wrong, sorry – gbn Jun 29 '10 at 21:41
  • Well, I'm going to keep toying with the CROSS APPLY, because everything I read about it seems to point towards this application. If you have any other thoughts on how to get my script to work, let me know! Thanks for your help so far. – Brett Jun 30 '10 at 13:13