Unix Technical Forum

Table variables

This is a discussion on Table variables within the SQL Server forums, part of the Microsoft SQL Server category; --> Hello, I am writing a function that uses two table variables. The structures of both are shown here: DECLARE ...


Go Back   Unix Technical Forum > Database Server Software > Microsoft SQL Server > SQL Server

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-29-2008, 07:16 PM
Andrew Clark
 
Posts: n/a
Default Table variables

Hello,

I am writing a function that uses two table variables. The structures of
both are shown here:

DECLARE @workdates TABLE (
conflict CHAR(1),
workdate SMALLDATETIME
)

DECLARE @existing TABLE (
workdate SMALLDATETIME
)

I need to do an update on the first table:

UPDATE @workdates
SET conflict = 'X'
FROM @existing s
WHERE workdate = s.workdate

I am concerned that the unqualified 'workdate' in the WHERE clause will
give me an ambiguous column reference. Is this SQL statement valid?

Thanks,
Andrew
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-29-2008, 07:16 PM
Alexander Kuznetsov
 
Posts: n/a
Default Re: Table variables

UPDATE @workdates
SET conflict = 'X'
FROM @workdates w, @existing s
WHERE w.workdate = s.workdate

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-29-2008, 07:16 PM
Andrew Clark
 
Posts: n/a
Default Re: Table variables

"Alexander Kuznetsov" <AK_TIREDOFSPAM@hotmail.COM> wrote in
news:1138895559.862884.111780@g47g2000cwa.googlegr oups.com:

> UPDATE @workdates
> SET conflict = 'X'
> FROM @workdates w, @existing s
> WHERE w.workdate = s.workdate
>
>


But is the original statement valid?

UPDATE @workdates
SET conflict = 'X'
FROM @existing s
WHERE workdate = s.workdate

Andrew
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 02-29-2008, 07:16 PM
Greg
 
Posts: n/a
Default Re: Table variables

Hi Andrew,

You could answer the question yourself by running the queries as a
script in QA.. E.G

--START ANDREWS SCRIPT

DECLARE @workdates TABLE (
conflict CHAR(1),
workdate SMALLDATETIME
)


DECLARE @existing TABLE (
workdate SMALLDATETIME
)

insert @workdates values ('a', '2006-02-03')
insert @existing values ('2006-02-03')
insert @workdates values ('a', '2006-02-02')

--I need to do an update on the first table:
UPDATE @workdates
SET conflict = 'X'
FROM @existing s
WHERE workdate = s.workdate

-- FINISH ANDREWS SCRIPT

You'll find this error message..

Server: Msg 209, Level 16, State 1, Line 17
Ambiguous column name 'workdate'.

That answers your original post. The following is a working example -
note the syntax differences..

-- START GREGS SCRIPT
DECLARE @workdates TABLE (
conflict CHAR(1),
workdate SMALLDATETIME
)


DECLARE @existing TABLE (
workdate SMALLDATETIME
)

insert @workdates values ('a', '2006-02-03')
insert @existing values ('2006-02-03')
insert @workdates values ('a', '2006-02-02')

--I need to do an update on the first table:
UPDATE w
SET conflict = 'X'
FROM @existing s JOIN @workdates w ON w.workdate = s.workdate

-- FINISH GREGS SCRIPT

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply

« perf | T-sql »

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 05:19 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com