01-21-2011 05:02 AM
Hi,
How can I execute a stored procedure which inserts a recording in a table of a data base with sql toolkit please ?
Thanks.
Karine
01-24-2011 03:36 AM
hi,
Thank you for posting on National Instrument's Forum.
Could you send me a Vi if you have already developed something?
What version of LabVIEW are you using?
Brice S.
National Instruments France
01-24-2011 04:04 AM
Hello,
Thanks for your help.
I don't use Labview but Labwindows/CVI 9.0. I created a stored procedure in my data base and I need an example to use this one in my program.
You'll find the stored procedure attached, which inserts a recording in a table of the data base.
For the moment in my program, I insert every recording by preparing and executing a query, and I would like to use a stored procedure to simplify it.
Best Regards.
Karine
01-24-2011 09:49 AM
hi,
I found an example to insert data in the database.
look at this example and return to me if this is not the answer.
Brice S.
National Instruments France
01-25-2011 03:53 AM
Hello,
Sorry but I work with LABWINDOWS/CVI 9.0 and not LABVIEW.
Your file don't help me, because I can't open it.
Have you other thing to help me please?
Thanks.
Karine.
01-26-2011 03:05 AM
hi,
After some research with a colleague I think you should use this example. You can find in <help>.
Brice S.
National Instruments France
01-26-2011 01:20 PM - edited 01-26-2011 01:23 PM
Hi Karine,
I think Brice is a little confused as to what you meant by a stored procedure, so that example program isn't really what you're looking for. Unfortunately we don't have any example programs displaying stored procedures. The various stored procedure functions themselves do have code snippets however. This one is from DBSetParamInt:
/* This example works with Microsoft SQL Server. */
/* Create a stored procedure with input, output, */
/* and return value parameters. */
resCode = DBImmediateSQL (hdbc, "create proc sp_AdoTest(@InParam int, @OutParam int OUTPUT ) \
AS SELECT * FROM meas WHERE val1 >= @InParam \
SET @OutParam = @InParam * 2 \
RETURN @InParam + 10");
/* Prepare a statement that calls the stored procedure. */
resCode = DBSetAttributeDefault (hdbc, ATTR_DB_COMMAND_TYPE, DB_COMMAND_STORED_PROC);
hstmt = DBPrepareSQL (hdbc, "sp_AdoTest");
resCode = DBSetAttributeDefault (hdbc, ATTR_DB_COMMAND_TYPE, DB_COMMAND_UNKNOWN);
/* Refresh the parameters from the stored procedure. */
resCode = DBRefreshParams (hstmt);
/* Set the input parameter. */
resCode = DBSetParamInt (hstmt, 2, 10);
/* Execute the statement. */
resCode = DBExecutePreparedSQL (hstmt);
while ((resCode = DBFetchNext (hstmt)) == DB_SUCCESS) {
/* Process records returned by the stored procedure. */
}
/* Close the statement. Output parameters are invalid */
/* until you close the statement. */
resCode = DBClosePreparedSQL (hstmt);
/* Examine the parameter values. */
resCode = DBGetParamInt (hstmt, 1, &retParam);
resCode = DBGetParamInt (hstmt, 2, &inParam);
resCode = DBGetParamInt (hstmt, 3, &outParam);
/* Discard the statement. */
hstmt = DBDiscardSQLStatement (hstmt);
Now obviously you have your stored procedure already created so you can skip the first DBImmediateSQL call which creates the stored procedure, but you'll still have to set the command types, "prepare" the SQL and refresh the params. You'd use multiple DBSetParamChar calls to pass in the ZUserID, ZUserName, ZPassword, etc. and then DBSetParamInt for ZUserGroup, ZValidity, .... You'll notice that the SQL toolkit does not have functions for "bit" or "boolean" datatypes built in. However to set your ZUserActive and ZVerifier params, you should be able to use the DBSetParamVariant function. Just declare two variants, and then do something like:
CA_VariantSetBool(&varActive, 1);
CA_VariantSetBool(&varVerifier, 0);
DBSetParamVariant(hstmt, 5, varActive);
DBSetParamVariant(hstmt, 10, varVerifier);
Once you have all your parameters set up, just call DBExecutePreparedSQL and everything should be good. One thing to note, the parameter Index for these functions is 1-based, but I'm pretty sure that the 1-index is always going to be the return value, even if your stored procedure doesn't return anything, so when setting your ZUserID it should start at index 2 and continue from there. I'm going off of memory on that, so if it's wrong I apologize.
At the moment I don't have a working database system set up to test this on, but if you need more help I can do that. Stored Procedures are pretty advanced concepts, so don't hesitate to ask.