Unix Technical Forum

Syntax for updating table variables

This is a discussion on Syntax for updating table variables within the SQL Server forums, part of the Microsoft SQL Server category; --> What would be the correct syntax, if any, that allows updating a table variable in a statement that uses ...


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

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-29-2008, 06:48 AM
Dimitri Furman
 
Posts: n/a
Default Syntax for updating table variables

What would be the correct syntax, if any, that allows updating a table
variable in a statement that uses the same table variable in a correlated
subquery? Here's an example:

DECLARE @t table (N1 int NOT NULL, N2 int NOT NULL)

UPDATE @t SET
N1 = (SELECT COUNT(1)
FROM @t AS t
WHERE t.N2 < @t.N2)

This doesn't compile, complaining about "variable @t" in the WHERE clause.

I'm not so interested in a way to rewrite this particular statement to make
it work, but rather in a general way to refer to table variables in the
contexts where correlation names cannot be used.

Thank you.

--
remove a 9 to reply by email
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-29-2008, 06:48 AM
Madhivanan
 
Posts: n/a
Default Re: Syntax for updating table variables


Try this

UPDATE @t SET N1 = (SELECT COUNT(1) FROM @t AS t
WHERE t.N2 < M.N2) from M

Madhivanan

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-29-2008, 06:48 AM
Dimitri Furman
 
Posts: n/a
Default Re: Syntax for updating table variables

On Mar 24 2005, 04:20 am, "Madhivanan" <madhivanan2001@gmail.com> wrote in
news:1111656040.992951.161790@o13g2000cwo.googlegr oups.com:

>
> Try this
>
> UPDATE @t SET N1 = (SELECT COUNT(1) FROM @t AS t
> WHERE t.N2 < M.N2) from M
>
> Madhivanan
>


This gives

Server: Msg 208, Level 16, State 1, Line 3
Invalid object name 'M'.

If, however, I change the FROM clause above to

FROM @t AS M

then it compiles, but I'm not too familiar with UPDATE ... FROM to tell if
the statement will do what it's meant to do. In any case, is there a way to
do this that doesn't involve proprietory syntax?

--
remove a 9 to reply by email
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 02-29-2008, 06:48 AM
--CELKO--
 
Posts: n/a
Default Re: Syntax for updating table variables

>> In any case, is there a way to do this that doesn't involve
proprietory syntax? <<

The table variable is highly proprietary initself, so the best way is
not to use table variable at all. Then you can use Standard SQL UPDATE
statements easily and your code port, be maintainable, work the same
way from one release to the next, etc.

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 02-29-2008, 06:48 AM
Hugo Kornelis
 
Posts: n/a
Default Re: Syntax for updating table variables

On Thu, 24 Mar 2005 12:43:40 -0000, Dimitri Furman wrote:

(snip)
>If, however, I change the FROM clause above to
>
>FROM @t AS M
>
>then it compiles, but I'm not too familiar with UPDATE ... FROM to tell if
>the statement will do what it's meant to do. In any case, is there a way to
>do this that doesn't involve proprietory syntax?


Hi Dimitri,

No, unfortunately there isn't. Of course, table variable are proprietary
themselves, so your code has to be adapted when you port it anyway - but
I admit that it would be nice if the standard syntax could be used.

Best, Hugo
--

(Remove _NO_ and _SPAM_ to get my e-mail address)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 02-29-2008, 06:48 AM
David Portas
 
Posts: n/a
Default Re: Syntax for updating table variables

Actually you can do this:

UPDATE @t SET N1 =
(SELECT COUNT(*)
FROM
(SELECT
N1 AS N3,
N2 AS N4
FROM @t)T
WHERE N4 < N2)

(I think I saw this suggestion from Steve Kass previously)

But as you suggest, it's pretty academic really.

--
David Portas
SQL Server MVP
--

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 02-29-2008, 06:48 AM
Hugo Kornelis
 
Posts: n/a
Default Re: Syntax for updating table variables

On 24 Mar 2005 13:25:34 -0800, David Portas wrote:

>Actually you can do this:

(snip)

Hi David,

Neat!

>But as you suggest, it's pretty academic really.


Yeah ... academic, but neat nonetheless.

Best, Hugo
--

(Remove _NO_ and _SPAM_ to get my e-mail address)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 02-29-2008, 06:49 AM
Madhivanan
 
Posts: n/a
Default Re: Syntax for updating table variables


Dimitry

That shold be

UPDATE @t SET N1 = (SELECT COUNT(1) FROM @t AS t
WHERE t.N2 < M.N2) from @t M

Madhivanan

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


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 08:30 AM.


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