| Author |
Topic |
|
Sirchrisak
Starting Member
19 Posts |
Posted - 2005-07-27 : 12:46:13
|
I have some base tables from which I created a view.I want to insert into these base tables by inserting into the view, my problem is that it gives an error; incorrect syntax. Here is my script for creating the view and inserting into the view CREATE view AccountPayment As SELECT TransactionTypeId,AccountNumber,PaymentTypeId, Accounts.TransactionId,TransactionClassId,TransactionOwnerId,TransactionDate,[Description]FROM [Transaction],Accountswhere PaymentTypeId=2with check option insert into AccountPayment (1,2378PK, 2, 2, 3, '2-04-05', 'somestuff' ) it is giving me error of incorect syntax near PKChris |
|
|
rockmoose
SQL Natt Alfen
3279 Posts |
Posted - 2005-07-27 : 12:51:21
|
| '2378PK',notice the ---> ' <----rockmoose |
 |
|
|
Lakeside
Starting Member
5 Posts |
Posted - 2005-07-27 : 13:23:03
|
| Insert Into AccountPayment (TransactionTypeId,AccountNumber,PaymentTypeId,TransactionId,TransactionClassId,TransactionOwnerId,TransactionDate,[Description])Values (1,2378PK, 2, 2, 3, '2-04-05', 'somestuff' ) |
 |
|
|
Ravenn
Starting Member
7 Posts |
Posted - 2005-07-27 : 14:50:11
|
| uhh.... don't you need a trigger to insert into view? |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2005-07-27 : 14:52:54
|
quote: Originally posted by Ravenn uhh.... don't you need a trigger to insert into view?
No.Tara |
 |
|
|
jsmith8858
Dr. Cross Join
7423 Posts |
Posted - 2005-07-27 : 16:12:19
|
| I would think you'd need a trigger in this case ... this view is a cross product between two tables! I haven't tried it, but that can't possibly be something you can INSERT into or UPDATE, can it?- Jeff |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2005-07-27 : 16:19:23
|
Yes in this case you would as you can't insert into a view when more than one table is involved. I was, however, answering the specific question and not as related to this thread which I should have elaborated although I didn't notice his/her view included more than one table as my eyes will see more than one table when the JOIN syntax is used. Tara |
 |
|
|
byrmol
Shed Building SQL Farmer
1591 Posts |
Posted - 2005-07-27 : 16:23:06
|
| That's right Jeff.SQL Server can only INSERT/UPDATE/DELETE views that reference a single table in the FROM clause.Anything else requires INSTEAD OF triggers.DavidMA front-end is something that tries to violate a back-end. |
 |
|
|
byrmol
Shed Building SQL Farmer
1591 Posts |
Posted - 2005-07-27 : 16:25:26
|
| SNIPED!No wonder you are at 11K Tara!DavidMA front-end is something that tries to violate a back-end. |
 |
|
|
rockmoose
SQL Natt Alfen
3279 Posts |
Posted - 2005-07-27 : 16:28:29
|
| EDIT:The UPDATE / INSERT / DELETE can only affect 1 base tablein the view.So You can modify views that reference several tables (in a way at least)Even CROSS JOINS.rockmoose |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2005-07-27 : 16:54:11
|
| I'm not sure what your point is moose. What part are you disagreeing with that David and I said?Tara |
 |
|
|
rockmoose
SQL Natt Alfen
3279 Posts |
Posted - 2005-07-27 : 17:14:05
|
quote: Originally posted by tduggan I'm not sure what your point is moose. What part are you disagreeing with that David and I said?Tara
I thought you guys meant that there can't be a JOIN clausein the view definition. (or implicit "old style" joins).-- At least if the view is going to be updateable wo iof triggers --Did I misinterpret?rockmoose |
 |
|
|
byrmol
Shed Building SQL Farmer
1591 Posts |
Posted - 2005-07-27 : 17:33:29
|
| Hmmm. I am confused rockmoose.My understanding is that if the view contains more than one table in the FROM clause (this includes any type of JOIN operation) the view MUST have INSTEAD OF triggers to be able to updated.DavidMA front-end is something that tries to violate a back-end. |
 |
|
|
rockmoose
SQL Natt Alfen
3279 Posts |
Posted - 2005-07-27 : 17:48:50
|
No, it can be inserted/updated as long as only 1 base table is affected. (delete not possible).You usually do WITH CHECK, might cause violations.You can try it out:create table x(a int primary key)create table y(b int primary key)gocreate view xy as select a,b from x,y -- cross join viewgoinsert xy(a) select 1insert xy(a) select 2insert xy(b) select 3insert xy(b) select 4select * from xyupdate xy set a=b --<-- fails on pkgoupdate xy set a=a+b --<-- okselect * from xygoupdate xy set a=2,b=2 --<-- fails on multiple base tablesgodrop table xdrop table ydrop view xy rockmoose |
 |
|
|
byrmol
Shed Building SQL Farmer
1591 Posts |
Posted - 2005-07-27 : 18:05:04
|
| Wow! I am not at my dev box.. so I'll try later.Does it work if you mark the view WITH CHECK?You learn something new everyday....DavidMA front-end is something that tries to violate a back-end. |
 |
|
|
rockmoose
SQL Natt Alfen
3279 Posts |
Posted - 2005-07-27 : 18:33:14
|
The example I posted is really simple, so it works with the WIH CHECK OPTION.In real life, I guess it will effectively cause inserts+updates on these kinds of views to fail.I don't really use that option, maybe I should...create table x(a int primary key)create table y(b int null)gocreate view xy as select a,b from x,y where a=b or b is nullgoinsert xy(a) select 1insert xy(b) select 1insert xy(b) select 1insert xy(b) select 1select * from xyupdate xy set b=null --<-- update the many sideselect * from xy--drop table x;drop table y;drop view xya b ----------- ----------- 1 11 11 1a b ----------- ----------- 1 NULL1 NULL1 NULL rockmoose |
 |
|
|
byrmol
Shed Building SQL Farmer
1591 Posts |
Posted - 2005-07-27 : 18:43:15
|
| >>I don't really use that option, maybe I should...That's all I use! Maybe I shouldn't!DavidMA front-end is something that tries to violate a back-end. |
 |
|
|
byrmol
Shed Building SQL Farmer
1591 Posts |
Posted - 2005-07-27 : 19:47:41
|
OK... I have had a little chance to play with this and it is seriously twisted..create table x(a int primary key)create table y(b int null)gocreate view xy as select a,b from x,y where a=2 gocreate view xy_CHECK as select a,b from x,y where a=2 WITH CHECK OPTIONgo--Inserts first (Not checked)insert xy(a) select 2 -- OKinsert xy(a) select 3 -- OK But should FAIL!!!!go--start againdelete xdelete ygo--CHECKED viewinsert xy_CHECK(a) select 2 --NO -- But should have WORKED! insert xy_CHECK(b) select 1 -- NOgo--Try again with rows in y -- y must not be empty!?!?!?!?!?!?insert xy(b) select 1 -- OKinsert xy_CHECK(a) select 2 --Yes!insert xy_CHECK(b) select 1 -- Yes!go--UpdatesUPDATE xy_CHECK set b = 100 -- OKgoUPDATE xy_CHECK set a = 1 -- BADgoUPDATE xy set a = 1 -- OK But should have FAILEDgoselect * from xyselect * from xy_CHECKselect * from yselect * from x--drop table x;drop table y;drop view xy, xy_CHECK The empty set of Y completely shattered the CHECK clause...DAMN!The violation of the views constraint is unnaceptable IMO and I would be VERY hard pressed to expose it to end users.... That's why I like the CHECK option, it allows end users to throw shit at it while maintaing integrity...Thanks for the lesson rockmoose!DavidMA front-end is something that tries to violate a back-end. |
 |
|
|
rockmoose
SQL Natt Alfen
3279 Posts |
Posted - 2005-07-28 : 07:56:06
|
| >> insert xy(a) select 3 -- OK But should FAIL!!!!I can't see that. Because of the where a = 2 ?, It doesn't check those kind of predicates afaik.Yes, this was fishy.It's always possible to create iof triggers with immediate raiserror/return on views that are not used for insert/update,and prevent the user from doing anything except selecting from them.This was all interesting, I think You tend to use views more than me though.When I use views it's mostly only for SELECT, certainly that is the case if the view involves a JOIN.Views that I design for update/insert are simple ones that are just a projection + restriction from 1 base table.rockmoose |
 |
|
|
|