Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Union problem 2

Status
Not open for further replies.

chicdog

Programmer
Feb 28, 2002
84
US
I keep getting the error 'Incorrect syntax near the keyword 'UNION'. What I'm trying to do is do an Union between two result sets. (FYI The SELECT Statements match)

SET @WhereClause ='AND (dbo.tblAttendEvent.EventKey IN (' + @WhereClause + '))'

DECLARE @SQL Varchar(3000)

SET @SQL='SELECT ..............
WHERE dbo.tblMeetingAttendee.MeetingKey ='
SET @SQL = @SQL + CONVERT(VARCHAR,@Meetingkey) + WhereClause
EXEC (@SQL)

UNION ALL

SET @SQL='SELECT ..............
WHERE dbo.tblMeetingAttendee.MeetingKey ='

SET @SQL= @SQL + CONVERT(VARCHAR,@Meetingkey) + @WhereClause
EXEC(@SQL)

RETURN

Thanks,
Bryan
 
Try this.

SET @WhereClause ='AND (dbo.tblAttendEvent.EventKey IN (' + @WhereClause + '))'

DECLARE @SQL Varchar(3000)

SET @SQL='SELECT ..............
WHERE dbo.tblMeetingAttendee.MeetingKey ='
SET @SQL = @SQL + CONVERT(VARCHAR,@Meetingkey) + WhereClause + ' UNION ALL '

SET @SQL=@SQL + ' SELECT ..............
WHERE dbo.tblMeetingAttendee.MeetingKey ='

SET @SQL= @SQL + CONVERT(VARCHAR,@Meetingkey) + @WhereClause
EXEC(@SQL)

RETURN

Sunil
 
Chikey,


your sql statement including the union must be a single string

Code:
declare @sql varchar(8000)
set @sql = 'select a from tab1 UNION ALL select a from tabb'


Glyndwr
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top