Showing posts with label update. Show all posts
Showing posts with label update. Show all posts

Monday, March 12, 2012

How best to update a record from another record in the same table?

(SQL Server 2000, SP3)
Hello all!
I have a seemingly easy question that seems to stump me whenever I come across it, and was
hoping to get some clarification.
Consider the following:
create table Test (Id int, Value1 sysname, Value2 sysname)
go
insert into Test values (1, 'Test1', 'Another1')
insert into Test values (2, 'Test2', 'Another2')
go
I would like to update the first record with some of the fields in the second record.
Part of my confusion lies in the fact that you can't alias the table to be UPDATEd. My
first attempt looks something like this:
update Test
set Test.Value2 = Value2
from Test as t2
where Test.Id = 1
and t2.Id = 2
Which gives me the following error:
Server: Msg 107, Level 16, State 3, Line 1
The column prefix 'Test' does not match with a table name or alias name used in the query.
So I tried to remove the Test reference in the WHERE clause:
update Test
set Test.Value2 = Value2
from Test as t2
where Id = 1
and t2.Id = 2
And this gives me 0 records affected -- perhaps like it's using the t2.Id implicitly.
I could do something like this:
update Test
set Test.Value2 = (select Value2 from Test where Id = 2)
where Id = 1
And that works, but I'd hate to reference Test for as many times as I might have fields to
update in the SET clause.
I'd appreciate any pointers that anyone might be able to provide! :-)
John PetersonEither one would do...
update t1
set Value2=t2.Value2
from Test t1, Test t2
where t1.Id=1 and t2.Id=2
update t1
set Value2=t2.Value2
from Test t1 join Test t2
on t1.Id=1 and t2.Id=2
-oj
RAC v2.2 & QALite!
http://www.rac4sql.net
"John Peterson" <j0hnp@.comcast.net> wrote in message
news:%23GZxFZsXDHA.2320@.TK2MSFTNGP12.phx.gbl...
> (SQL Server 2000, SP3)
> Hello all!
> I have a seemingly easy question that seems to stump me whenever I come
across it, and was
> hoping to get some clarification.
> Consider the following:
> create table Test (Id int, Value1 sysname, Value2 sysname)
> go
> insert into Test values (1, 'Test1', 'Another1')
> insert into Test values (2, 'Test2', 'Another2')
> go
> I would like to update the first record with some of the fields in the
second record.
> Part of my confusion lies in the fact that you can't alias the table to be
UPDATEd. My
> first attempt looks something like this:
> update Test
> set Test.Value2 = Value2
> from Test as t2
> where Test.Id = 1
> and t2.Id = 2
> Which gives me the following error:
> Server: Msg 107, Level 16, State 3, Line 1
> The column prefix 'Test' does not match with a table name or alias name
used in the query.
> So I tried to remove the Test reference in the WHERE clause:
> update Test
> set Test.Value2 = Value2
> from Test as t2
> where Id = 1
> and t2.Id = 2
> And this gives me 0 records affected -- perhaps like it's using the t2.Id
implicitly.
> I could do something like this:
> update Test
> set Test.Value2 = (select Value2 from Test where Id = 2)
> where Id = 1
> And that works, but I'd hate to reference Test for as many times as I
might have fields to
> update in the SET clause.
> I'd appreciate any pointers that anyone might be able to provide! :-)
> John Peterson
>|||Thanks, oj!
I didn't realize that you could do an alias after the UPDATE, and then specify the actual
table name in the FROM clause. Very interesting!
Thanks again!
John Peterson
"oj" <nospam_ojngo@.home.com> wrote in message
news:%23Ya0TdsXDHA.652@.TK2MSFTNGP10.phx.gbl...
> Either one would do...
> update t1
> set Value2=t2.Value2
> from Test t1, Test t2
> where t1.Id=1 and t2.Id=2
> update t1
> set Value2=t2.Value2
> from Test t1 join Test t2
> on t1.Id=1 and t2.Id=2
>
> --
> -oj
> RAC v2.2 & QALite!
> http://www.rac4sql.net
>
> "John Peterson" <j0hnp@.comcast.net> wrote in message
> news:%23GZxFZsXDHA.2320@.TK2MSFTNGP12.phx.gbl...
> > (SQL Server 2000, SP3)
> >
> > Hello all!
> >
> > I have a seemingly easy question that seems to stump me whenever I come
> across it, and was
> > hoping to get some clarification.
> >
> > Consider the following:
> >
> > create table Test (Id int, Value1 sysname, Value2 sysname)
> > go
> >
> > insert into Test values (1, 'Test1', 'Another1')
> > insert into Test values (2, 'Test2', 'Another2')
> > go
> >
> > I would like to update the first record with some of the fields in the
> second record.
> >
> > Part of my confusion lies in the fact that you can't alias the table to be
> UPDATEd. My
> > first attempt looks something like this:
> >
> > update Test
> > set Test.Value2 = Value2
> > from Test as t2
> > where Test.Id = 1
> > and t2.Id = 2
> >
> > Which gives me the following error:
> >
> > Server: Msg 107, Level 16, State 3, Line 1
> > The column prefix 'Test' does not match with a table name or alias name
> used in the query.
> >
> > So I tried to remove the Test reference in the WHERE clause:
> >
> > update Test
> > set Test.Value2 = Value2
> > from Test as t2
> > where Id = 1
> > and t2.Id = 2
> >
> > And this gives me 0 records affected -- perhaps like it's using the t2.Id
> implicitly.
> >
> > I could do something like this:
> >
> > update Test
> > set Test.Value2 = (select Value2 from Test where Id = 2)
> > where Id = 1
> >
> > And that works, but I'd hate to reference Test for as many times as I
> might have fields to
> > update in the SET clause.
> >
> > I'd appreciate any pointers that anyone might be able to provide! :-)
> >
> > John Peterson
> >
> >
>|||You're welcome, John.
I believe you can find the example and some info regarding this syntax in
sql book online under "update".
--
-oj
Rac v2.2 & QALite
http://www.rac4sql.net
"John Peterson" <j0hnp@.comcast.net> wrote in message
news:O9fn6fsXDHA.2204@.TK2MSFTNGP12.phx.gbl...
> Thanks, oj!
> I didn't realize that you could do an alias after the UPDATE, and then
specify the actual
> table name in the FROM clause. Very interesting!
> Thanks again!
> John Peterson
> "oj" <nospam_ojngo@.home.com> wrote in message
> news:%23Ya0TdsXDHA.652@.TK2MSFTNGP10.phx.gbl...
> > Either one would do...
> >
> > update t1
> > set Value2=t2.Value2
> > from Test t1, Test t2
> > where t1.Id=1 and t2.Id=2
> >
> > update t1
> > set Value2=t2.Value2
> > from Test t1 join Test t2
> > on t1.Id=1 and t2.Id=2
> >
> >
> >
> > --
> > -oj
> > RAC v2.2 & QALite!
> > http://www.rac4sql.net
> >
> >
> >
> > "John Peterson" <j0hnp@.comcast.net> wrote in message
> > news:%23GZxFZsXDHA.2320@.TK2MSFTNGP12.phx.gbl...
> > > (SQL Server 2000, SP3)
> > >
> > > Hello all!
> > >
> > > I have a seemingly easy question that seems to stump me whenever I
come
> > across it, and was
> > > hoping to get some clarification.
> > >
> > > Consider the following:
> > >
> > > create table Test (Id int, Value1 sysname, Value2 sysname)
> > > go
> > >
> > > insert into Test values (1, 'Test1', 'Another1')
> > > insert into Test values (2, 'Test2', 'Another2')
> > > go
> > >
> > > I would like to update the first record with some of the fields in the
> > second record.
> > >
> > > Part of my confusion lies in the fact that you can't alias the table
to be
> > UPDATEd. My
> > > first attempt looks something like this:
> > >
> > > update Test
> > > set Test.Value2 = Value2
> > > from Test as t2
> > > where Test.Id = 1
> > > and t2.Id = 2
> > >
> > > Which gives me the following error:
> > >
> > > Server: Msg 107, Level 16, State 3, Line 1
> > > The column prefix 'Test' does not match with a table name or alias
name
> > used in the query.
> > >
> > > So I tried to remove the Test reference in the WHERE clause:
> > >
> > > update Test
> > > set Test.Value2 = Value2
> > > from Test as t2
> > > where Id = 1
> > > and t2.Id = 2
> > >
> > > And this gives me 0 records affected -- perhaps like it's using the
t2.Id
> > implicitly.
> > >
> > > I could do something like this:
> > >
> > > update Test
> > > set Test.Value2 = (select Value2 from Test where Id = 2)
> > > where Id = 1
> > >
> > > And that works, but I'd hate to reference Test for as many times as I
> > might have fields to
> > > update in the SET clause.
> > >
> > > I'd appreciate any pointers that anyone might be able to provide! :-)
> > >
> > > John Peterson
> > >
> > >
> >
> >
>

Friday, March 9, 2012

How ASP into ASP.NET with SQL server 2000

Dear All,
I'm new on ASP.NET, now trying convert ASP with SQL 2000 into ASP.NET SQL 2000 server.
I facing a problem while read, write, update the SQL command.
like,
rs.open("Select * from tableA"),ocoon, permission,permission
If rs.eof then
rs.addnew()
rs.column1 = "1"
else
rs.column2 = "2"
rs.column3 = "3"
rs.update()
end if
rs.close()
set rs = nothing
this is how to change into ASP.NET with SQL selection and updating command??
Regards,
I would recommend reading up some articles on Data Access in ASP.NET. There are some Tutorials on this site.

Wednesday, March 7, 2012

How about cascade insert and update

I am desgining a new database.
Is it suitable to set the cascade insert and cascade update to the foreign
key constrain?
While this can be done on the application level, or through triggers it is
best to do it through cascading update and deletes. Please refer to
http://support.microsoft.com/default...NoWebContent=1
for more information.
Hilary Cotter
Director of Text Mining and Database Strategy
RelevantNOISE.Com - Dedicated to mining blogs for business intelligence.
This posting is my own and doesn't necessarily represent RelevantNoise's
positions, strategies or opinions.
Looking for a SQL Server replication book?
http://www.nwsu.com/0974973602.html
Looking for a FAQ on Indexing Services/SQL FTS
http://www.indexserverfaq.com
"ad" <flying@.wfes.tcc.edu.tw> wrote in message
news:egYkwM1PGHA.648@.TK2MSFTNGP14.phx.gbl...
>I am desgining a new database.
> Is it suitable to set the cascade insert and cascade update to the foreign
> key constrain?
>
|||"ad" <flying@.wfes.tcc.edu.tw> wrote in message
news:egYkwM1PGHA.648@.TK2MSFTNGP14.phx.gbl...
>I am desgining a new database.
> Is it suitable to set the cascade insert and cascade update to the foreign
> key constrain?
>
"Cascade insert" doesn't exist. "Cascade delete" is useful, and you should
use it on foreign keys between related entities, but not on foreign keys to
reference or "lookup" tables. "Cascade updates" should almost never be
used, since they only apply where you are updating a primary key, which you
should almost never do.
David

How about cascade insert and update

I am desgining a new database.
Is it suitable to set the cascade insert and cascade update to the foreign
key constrain?While this can be done on the application level, or through triggers it is
best to do it through cascading update and deletes. Please refer to
http://support.microsoft.com/defaul...&NoWebContent=1
for more information.
Hilary Cotter
Director of Text Mining and Database Strategy
RelevantNOISE.Com - Dedicated to mining blogs for business intelligence.
This posting is my own and doesn't necessarily represent RelevantNoise's
positions, strategies or opinions.
Looking for a SQL Server replication book?
http://www.nwsu.com/0974973602.html
Looking for a FAQ on Indexing Services/SQL FTS
http://www.indexserverfaq.com
"ad" <flying@.wfes.tcc.edu.tw> wrote in message
news:egYkwM1PGHA.648@.TK2MSFTNGP14.phx.gbl...
>I am desgining a new database.
> Is it suitable to set the cascade insert and cascade update to the foreign
> key constrain?
>|||"ad" <flying@.wfes.tcc.edu.tw> wrote in message
news:egYkwM1PGHA.648@.TK2MSFTNGP14.phx.gbl...
>I am desgining a new database.
> Is it suitable to set the cascade insert and cascade update to the foreign
> key constrain?
>
"Cascade insert" doesn't exist. "Cascade delete" is useful, and you should
use it on foreign keys between related entities, but not on foreign keys to
reference or "lookup" tables. "Cascade updates" should almost never be
used, since they only apply where you are updating a primary key, which you
should almost never do.
David

How about cascade insert and update

I am desgining a new database.
Is it suitable to set the cascade insert and cascade update to the foreign
key constrain?While this can be done on the application level, or through triggers it is
best to do it through cascading update and deletes. Please refer to
http://support.microsoft.com/default.aspx?scid=http://support.microsoft.com:80/support/kb/articles/Q142/4/80.asp&NoWebContent=1
for more information.
--
Hilary Cotter
Director of Text Mining and Database Strategy
RelevantNOISE.Com - Dedicated to mining blogs for business intelligence.
This posting is my own and doesn't necessarily represent RelevantNoise's
positions, strategies or opinions.
Looking for a SQL Server replication book?
http://www.nwsu.com/0974973602.html
Looking for a FAQ on Indexing Services/SQL FTS
http://www.indexserverfaq.com
"ad" <flying@.wfes.tcc.edu.tw> wrote in message
news:egYkwM1PGHA.648@.TK2MSFTNGP14.phx.gbl...
>I am desgining a new database.
> Is it suitable to set the cascade insert and cascade update to the foreign
> key constrain?
>|||"ad" <flying@.wfes.tcc.edu.tw> wrote in message
news:egYkwM1PGHA.648@.TK2MSFTNGP14.phx.gbl...
>I am desgining a new database.
> Is it suitable to set the cascade insert and cascade update to the foreign
> key constrain?
>
"Cascade insert" doesn't exist. "Cascade delete" is useful, and you should
use it on foreign keys between related entities, but not on foreign keys to
reference or "lookup" tables. "Cascade updates" should almost never be
used, since they only apply where you are updating a primary key, which you
should almost never do.
David

Monday, February 27, 2012

hotfix/service pack directory

I remember seeing a post in this newsgroup for a website maintaining a
(fairly) comprehensive list of hotfixes, service packs and cumulative
update packs for SQL Server.
Anyone care to re-post?Maybe Aaron Bertrand's blog?
http://sqlblog.com/blogs/aaron_bertrand/default.aspx
Ben Nevarez
Senior Database Administrator
AIG SunAmerica
"horseradish" wrote:
> I remember seeing a post in this newsgroup for a website maintaining a
> (fairly) comprehensive list of hotfixes, service packs and cumulative
> update packs for SQL Server.
> Anyone care to re-post?
>|||take a look here
http://www.aspfaq.com/sql2005/show.asp?id=20
and here
http://www.aspfaq.com/sql2000Builds.asp
Denis The SQL Menace
http://sqlservercode.blogspot.com
http://sqlblog.com/blogs/denis_gobo/default.aspx
On Nov 27, 4:34 pm, horseradish <dontcal...@.illcallu.net> wrote:
> I remember seeing a post in this newsgroup for a website maintaining a
> (fairly) comprehensive list of hotfixes, service packs and cumulative
> update packs for SQL Server.
> Anyone care to re-post?|||Check this one
http://sqlsecurity.com/FAQs/SQLServerVersionDatabase/tabid/63/Default.aspx
Manu
"horseradish" wrote:
> I remember seeing a post in this newsgroup for a website maintaining a
> (fairly) comprehensive list of hotfixes, service packs and cumulative
> update packs for SQL Server.
> Anyone care to re-post?
>|||It must be this one =)
http://sqlserverbuilds.blogspot.com/
--
Ekrem Önsoy
"horseradish" <dontcallme@.illcallu.net> wrote in message
news:Ot30H1TMIHA.6060@.TK2MSFTNGP05.phx.gbl...
>I remember seeing a post in this newsgroup for a website maintaining a
>(fairly) comprehensive list of hotfixes, service packs and cumulative
>update packs for SQL Server.
> Anyone care to re-post?