Goran,
would it be possible to do this::
sql = "INSERT INTO studres ( uur, pcnr) SELECT uur from uur where
range='" & v & "', pcnr FROM pc where naam='" & w & "';"
Thanks
"Göran Andersson" <
[email protected]> schreef in bericht
Specify the data type for the parameters.
Example:
comd.Parameters.Add("@lol", SqlDbType.NVarChar. 25).Value = lol
Parameters are named using @. Square brackets are used to specify that a
string is a name. It's used for names with non-alphanumeric characters,
like [current-price], or names that are keywords, like [from].
Bart wrote:
Thanks to all ...
But in fact, it's not so simple, because there are other parameters
needed. What i try to do is:
sql = "INSERT INTO studres (logon, dag, uur, type, pcnr) SELECT [lol]
, [dag], [uur],'l', pcnr FROM pc where lokl='" & w & "';"
comd = New SqlCommand(sql, oConnection)
comd.Parameters.AddWithValue("[lol]", lol)
comd.Parameters.AddWithValue("[dag]", tda)
comd.Parameters.AddWithValue("[uur]", vv)
comd.ExecuteNonQuery()
but this gives the error:"Incorrect syntax near 'nvarchar'.
In sql server, field login is nvarchar(25), field dag is Datetime and
field uur is int
...
"Cowboy (Gregory A. Beamer)" <
[email protected]>
schreef in bericht
Not sure why you want to loop to do this, as this can all be done on
the SQL side, with a temp table, unless you are persisting the answers
for a long time. Even with a permanent table, you can clear the table
before rerunning this operation. If you need to know which number the
record is, you can use an IDENTITY field, in SQL Server (have to work
from DUAL in Oracle) or autonumber in Access.
This is the proc in SQL Server:
CREATE PROCEDURE TestProc
(
@w int -- Not sure of type here
)
AS
INSERT INTO MyTable (pcnr)
SELECT pcnr FROM pc
WHERE lokl = @w
SELECT @@COUNT
You now can use the return value for count and run the insert on all
records at the same time. The syntax is much easier.
1. Set up connection
2. Set up command
3. Set command as CommandType.StoredProcedure
4. Run ExecuteScalar on Command and retrieve count of records changed
Very effficient and much easier to manage.
--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA
*********************************************
Think outside the box!
*********************************************
Hi,
i get the error:
"There is already an open DataReader associated with this Command
which must be closed first"
Thanks
Bart
-----------------------------------------
Imports System.Data.sqlclient
Imports System.Data
...
...
sql = "SELECT count(*) FROM pc where lokl='" & w & "';"
comd = New SqlCommand(sql, oConnection)
recpc = comd.ExecuteScalar
sql = "SELECT pcnr FROM pc where lokl='" & w & "';"
comd = New SqlCommand(sql, oConnection)
dtreader = comd.ExecuteReader
for j = 1 To recpc
dtreader.Read()
ww = dtreader.GetValue(0)
sql = "INSERT INTO mytable (field1 values(" & ww & ")"
comd.ExecuteNonQuery()
Next
dtreader.Close()