Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
DaveBF
Yak Posting Veteran
89 Posts |
Posted - 2010-05-05 : 11:50:26
|
| Hi,I'm getting an error: Incorrect syntax near ',' on the rather long statement below. When I double click on the error, the line I show with a blank line on each side, gets highlighted. Can I get some help with this? You should be able to just copy/paste this into SSMS and see the same error. Thank you!exec sp_executesql N'INSERT INTO [Position] ([Pos_ViceClassification_ID], [Pos_Request_ID], [T_and_L], [Pos_Parent_ID], [FTEE], [Type], [Organization], [Pos_LU_Services_ID], [Pos_Section_ID], [Pos_LU_PositionsAndCodes_ID], [Pos_LU_DutyStations_ID]) SELECT ''3'', @Pos_Request_ID, ''3'', ''3'', @FTEE, @Type, ''VA Boston Healthcare System'', LU_Services_ID FROM LU_Services WHERE ServiceName=@ServiceName and OrgCode=@OrgCode, ''3'', LU_PositionsAndCodes_ID FROM LU_PositionsAndCodes WHERE PositionTitle=@PositionTitle, LU_DutyStations_ID FROM LU_DutyStations WHERE DutyStation=@DutyStation',N'@Pos_ViceClassification_ID int,@Pos_Request_ID int,@T_and_L nvarchar(1),@Pos_Parent_ID int,@FTEE decimal(1,0),@Pos_Section_ID int,@Type nvarchar(4000),@ServiceName nvarchar(16),@OrgCode nvarchar(16),@PositionTitle nvarchar(16),@DutyStation nvarchar(16)',@Pos_ViceClassification_ID=1,@Pos_Request_ID=NULL,@T_and_L=N'1',@Pos_Parent_ID=1,@FTEE=1,@Pos_Section_ID=1,@Type=NULL,@ServiceName=N'Please select...',@OrgCode=N'ABC',@PositionTitle=N'DEF',@DutyStation=N'GHI' |
|
|
Transact Charlie
Master Smack Fu Yak Hacker
3451 Posts |
Posted - 2010-05-05 : 12:00:38
|
Well with a little formatting this would have been easy to debug. You can use character returns in dynamic sql!exec sp_executesql N'INSERT INTO [Position] ( [Pos_ViceClassification_ID] , [Pos_Request_ID] , [T_and_L] , [Pos_Parent_ID] , [FTEE] , [Type] , [Organization] , [Pos_LU_Services_ID] , [Pos_Section_ID] , [Pos_LU_PositionsAndCodes_ID] , [Pos_LU_DutyStations_ID] )SELECT ''3'' , @Pos_Request_ID , ''3'' , ''3'' , @FTEE , @Type , ''VA Boston Healthcare System'' , LU_Services_IDFROM LU_ServicesWHERE ServiceName=@ServiceName and OrgCode=@OrgCode, ''3'', LU_PositionsAndCodes_ID FROM LU_PositionsAndCodes WHERE PositionTitle=@PositionTitle, LU_DutyStations_ID FROM LU_DutyStations WHERE DutyStation=@DutyStation',N'@Pos_ViceClassification_ID int,@Pos_Request_ID int,@T_and_L nvarchar(1),@Pos_Parent_ID int,@FTEE decimal(1,0),@Pos_Section_ID int,@Type nvarchar(4000),@ServiceName nvarchar(16),@OrgCode nvarchar(16),@PositionTitle nvarchar(16),@DutyStation nvarchar(16)',@Pos_ViceClassification_ID=1,@Pos_Request_ID=NULL,@T_and_L=N'1',@Pos_Parent_ID=1,@FTEE=1,@Pos_Section_ID=1,@Type=NULL,@ServiceName=N'Please select...',@OrgCode=N'ABC',@PositionTitle=N'DEF',@DutyStation=N'GHI' It scan's ok up until the bit in red. I'm not sure what you were trying to do there but the syntax is all wrong there.Charlie===============================================================Msg 3903, Level 16, State 1, Line 1736The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION |
 |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2010-05-05 : 12:01:03
|
Your SELECT statement is complete bullsh.. - sorry.Read about joining tables. No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
DaveBF
Yak Posting Veteran
89 Posts |
Posted - 2010-05-05 : 13:25:18
|
| Charlie,Thanks for your help.What I'm trying to do is an Insert statement, specifying 11 fields, some of which are hardcoded, some are done through parameters, and some are done through (secondary?) Select FROM/WHERE clauses. Is this possible? I started reformatting as you described, and still didn't see the problem. Am I missing the word "Select" on my secondary Select from/where clauses? Let me know if this is not clear. For example, Say I want to create a name record where firstname comes out of 1 table, lastname out of a different table, and middle, is "B".Insert into TABLE (firstname, lastname, middle) Select firstname from TABLE2 where SSN='123', lastnamme from TABLE3 where SSN='123,'B'Am I missing the word VALUES? Do I need the word SELECT for the second item? Thanks,Dave |
 |
|
|
DaveBF
Yak Posting Veteran
89 Posts |
Posted - 2010-05-05 : 13:27:30
|
quote: Originally posted by X002548 WHY are you using Dynamic SQL?Brett8-)
It's what gets generated from by Visual Studio. |
 |
|
|
DaveBF
Yak Posting Veteran
89 Posts |
Posted - 2010-05-05 : 13:41:27
|
quote: Originally posted by webfred Your SELECT statement is complete bullsh.. - sorry.Read about joining tables. No, you're never too old to Yak'n'Roll if you're too young to die.
Thanks for your help. :-)I don't think I want to do a join here. I'm doing an insert where some of the fields, are being pulled from another table (using a select/from/where clause) In a simple case, I just did:insert into temp(field1,field2)select '3', last from Person where Person_ID='1234'and that worked. I think my problem is the syntax for the dynamic sql, which I'm using because I'm doing this through visual studio in an .asmx page. |
 |
|
|
Kristen
Test
22859 Posts |
Posted - 2010-05-05 : 14:04:14
|
IF you want to select data from two tables you need a join.SELECT A.Col1, A.Col2, B.ColX, B.ColYFROM TableA AS A JOIN TableB AS B ON B.SomeIDColumn = A.SomeOtherIDColumn AND B.OptionalOtherColumn = A.CorrespondingColumnName |
 |
|
|
DaveBF
Yak Posting Veteran
89 Posts |
Posted - 2010-05-05 : 15:52:07
|
quote: Originally posted by Kristen IF you want to select data from two tables you need a join.SELECT A.Col1, A.Col2, B.ColX, B.ColYFROM TableA AS A JOIN TableB AS B ON B.SomeIDColumn = A.SomeOtherIDColumn AND B.OptionalOtherColumn = A.CorrespondingColumnName
I still don't think I need to do a Join for this. I made a much simpler case that shows what I'm trying to do:This statement works:insert into mytable(field1,field2) select '3', last from Person where Person_ID = '9169'It creates a record in mytable with (3, Jones)But this statement does not work. It gives a syntax error. I would expect it to create a record with (Jones,3):insert into mytable(field1,field2) select last from Person where Person_ID = '9169', '3'Can you tell me why one works but not the other, and how I would do what I'm trying to do in the second case? -Thanks |
 |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
Kristen
Test
22859 Posts |
Posted - 2010-05-06 : 02:11:52
|
| "insert into mytable(field1,field2) select last from Person ..."Your statement declares the two fields that will be inserted into MyTable, but you have only specified one field in your select."... where Person_ID = '9169', '3'"I have no idea what you intend that this should do, but its not valid syntax.If you explain what you want, rather than your proposed solution, folk will make suggestions. |
 |
|
|
DaveBF
Yak Posting Veteran
89 Posts |
Posted - 2010-05-06 : 07:22:27
|
quote: Originally posted by Kristen "insert into mytable(field1,field2) select last from Person ..."If you explain what you want, rather than your proposed solution, folk will make suggestions.
Kristen,The SQL statement that I originally posted was created by Visual Studio, based on an InsertCommand line, in my aspx file. But here's what I want. I appreciate the help.I have a SqlDataSource in my aspx which has an InsertCommand, containing several ControlParameters, plus a few of the items are fields from other tables, also using ControlParameters.So, for example, I'm creating a Person record, where the name is from a Text field (via a ControlParameter), and the City, might be a lookup from a different table, based on a zipcode in a text field. So, I would (incorrectly)change my InsertCommand to be:InsertCommand="INSERT INTO PERSON (Name,City) SELECT @NAME, CITY FROM CITYTABLE WHERE ZIP=@ZIP<InsertParameters>asp:ControlParameter Name="Name",Type="String",PropertyName="Text",ControlID="TextBoxName"asp:ControlParameter Name="Zip",Type="String",PropertyName="Text",ControlID="TextBoxZip"</InsertParameteters>How would you do this?-Dave |
 |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
DaveBF
Yak Posting Veteran
89 Posts |
Posted - 2010-05-06 : 11:02:02
|
| I'm not ready to give up. If I can figure out the SQL syntax, I can get the Visual Studio to generate it. I found another post in this forum which was very similar to what I want to do. It contains this:--------------------------------------------------------------------------------Try this: INSERT INTO Members ( memberID, memberareaID )SELECT @memberID, memberareaID FROM MemberAreas WHERE areadescription = @areadescription--------------------------------------------------And this syntax seems to be ok.But in the Select, what if the order is reversed? That is, what if the first item is the one that requires the secondary table lookup? This gives a syntax error:INSERT INTO Members ( memberID, memberareaID )SELECT memberareaID FROM MemberAreas WHERE areadescription = @areadescription, @memberIDIt says, Incorrect syntax near ','. and that is the problem I'm trying to solve. |
 |
|
|
DaveBF
Yak Posting Veteran
89 Posts |
Posted - 2010-05-06 : 11:54:19
|
quote: Originally posted by DaveBF I'm not ready to give up. If I can figure out the SQL syntax, I can get the Visual Studio to generate it. I found another post in this forum which was very similar to what I want to do. It contains this:--------------------------------------------------------------------------------Try this: INSERT INTO Members ( memberID, memberareaID )SELECT @memberID, memberareaID FROM MemberAreas WHERE areadescription = @areadescription--------------------------------------------------And this syntax seems to be ok.But in the Select, what if the order is reversed? That is, what if the first item is the one that requires the secondary table lookup? This gives a syntax error:INSERT INTO Members ( memberID, memberareaID )SELECT memberareaID FROM MemberAreas WHERE areadescription = @areadescription, @memberIDIt says, Incorrect syntax near ','. and that is the problem I'm trying to solve.
ok, I solved it. I was missing the word 'Select' on my Sub-Selects.Thank you for all your responses. Except for the ones about why I shouldn't be using Visual Studio to generate my sql. |
 |
|
|
Kristen
Test
22859 Posts |
Posted - 2010-05-06 : 13:50:53
|
Could have done with seeing how you solved it so we could comment on whether that was "best way", or not.In addition to SELECT you would need some parenthesis and a close double-quote:InsertCommand="INSERT INTO PERSON (Name,City) SELECT @NAME, (SELECT CITY FROM CITYTABLE WHERE ZIP=@ZIP)" You need to be certain that the sub-query cannot contain multiple rows, otherwise it will raise an error at runtime. (Having a unique index on ZIP would do the trick)."Except for the ones about why I shouldn't be using Visual Studio to generate my sql"Don't forget to come back and tell Brett he was right ... when you discover that he was if you do any serious amount of SQL you will come to that conclusion in the end, Brett tends not to be interested in the "bit in the middle" of any journey from A-to-B |
 |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
Kristen
Test
22859 Posts |
Posted - 2010-05-06 : 15:56:07
|
"We'll take about it for 5 minutes and then do it Kristen's way" .. that one? |
 |
|
|
|
|
|
|
|