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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Union with Variables

Status
Not open for further replies.

briangriffin

Programmer
Nov 6, 2008
878
US
SQL Server 2005

Trying to do a union with variables:

declare @v_datecutoff datetime

declare @v_table
(value1)

insert into @v_table
select value from sometable where datevalue < @v_datecutoff


UNION ALL

declare @v_datecutoff2 datetime

declare @v_table2
(value1)

insert into @v_table2
select value from sometable2 where datevalue < @v_datecutoff2


-----------------
Each query runs individually, but when running the entire query I get "Incorrect syntax near the keyword declare", and it's pointing to the first word of the second query.

What's going on, and what can I do about it?

Thanks in advance.
 
Please read BOL (SQL Server 2005 Books Online ) link
ms-help://MS.SQLCC.v9/MS.SQLSVR.v9.en/tsqlref9/html/607c296f-8a6a-49bc-975a-b8d0c0914df7.htm

Specifically the definition
UNION
Combines the results of two or more queries into a single result set that includes all the rows that belong to all queries in the union. The UNION operation is different from using joins that combine columns from two tables.
 
It would help if I included the entire query - I left out the last line of each query.

declare @v_datecutoff datetime

declare @v_table
(value1)

insert into @v_table
select value from sometable where datevalue < @v_datecutoff

select * from @v_table


UNION ALL

declare @v_datecutoff2 datetime

declare @v_table2
(value1)

insert into @v_table2
select value from sometable2 where datevalue < @v_datecutoff2

select * from @v_table2

-------------------

So I really am trying to do a union, and the results of each individual query returns the same number and datatype of fields.

Sorry for the incomplete post, but the problem remains.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top