views:

112

answers:

2

How to I pass this to SQL Server, it seems to want the "Go 4" on a second line?

Insert Into tbl Values (896,0) GO 6

A: 

Your question is a little confusing from where I stand, but if you're trying to insert more than one record, I use something like this at work in MS SQL Server 2K all the time:

INSERT INTO table (field1, field2)
SELECT 'Value1', 'Value2'
UNION SELECT 'Value3', 'Value4'
Lazy Bob
How does this work? and what are Value3 and Value4
bochur1
I was sort of guessing that you were trying to insert more than one row into the table. If that is that case, then values 3 and 4 would be the values in the second row that you wanted to insert. If you're still now getting the answer you want, you may want to add a more concrete example to your original question, to clarify exactly what you're trying to do.
Lazy Bob
OK. I will post again the question with better explanation. I have situations that I need to write multiple rows of the same value to setup some files. Say I have to address 120 rows with two columns populated. I am looking for a shortcut, instead of having the Insert line repeated n times. How to do this?
bochur1
Ok, this seems kind of odd, but it sounds like what you're saying is that you want to insert an arbitrary number of rows of the same data. If that's the case, there is probably a better to accomplish your true end goal, but you should be able to insert an arbitrary number of rows as follows:`table` is the table to insert to, field1 and field2 are the field names, and value1 and value2 are the two values that you're going to insert a bunch of times:INSERT INTO table(field1,field2)SELECT 'value1','value2'UNION ALL SELECT 'value1','value2'UNION ALL SELECT 'value1','value2'and on and on
Lazy Bob
almost forgot, if you want to get slick, you could probably build some dynamic sql and use the REPLICATE() function to create your "UNION ALL SELECT 'value1','value2'" statement however many times you want. Google the REPLICATE() function for more details on it... I'm no expert with MS SQL.
Lazy Bob
+1  A: 

Are you sure you want to do this? The documentation says:

GO is not a Transact-SQL statement; it is a command recognized by the sqlcmd and osql utilities and SQL Server Management Studio Code editor. [...] Applications based on the ODBC or OLE DB APIs receive a syntax error if they try to execute a GO command.

If you are still sure you need this, use the vbCrLf constant to insert a linebreak:

Dim sql As String = "Insert Into tbl Values (896,0)" & vbCrLf & "GO 6"
Heinzi
GO is a batch separator in simple :)
anishmarokey
thanks for your explinationbtw, I had tried vbcrlf but that didn't help.
bochur1
Given what I quoted from the documentation, it's not surprising that it didn't work. ;-)
Heinzi