INSERT and SELECT in a single query. ADODB throws "Operation is not allowed when the object is closed" - sql-server

I have 2 queries, both work fine in SQL Studio:
Query 1:
SELECT [id],[fullname] FROM persons WHERE [id] IN (5802824683954111059,1615647673927737)
Query 2:
IF OBJECT_ID('tempdb..#temp_table_1') IS NOT NULL
TRUNCATE TABLE #temp_table_1
ELSE
CREATE TABLE #temp_table_1 ( [key] bigint primary key );
INSERT INTO #temp_table_1 ([key]) VALUES (5802824683954111059),(1615647673927737);
SELECT [id],[fullname] FROM persons WHERE [id] IN (SELECT [key] FROM #temp_table_1)
But when I execute these queries using ADODB.Recordset.Open() method, only the first query returns valid result.
For the second query Recordset.EOF property throws "Operation is not allowed when the object is closed" error.
What is wrong with the second query? Does ADODB support multiple statements in a query?
The sample code demonstrating the problem:
conn = new ActiveXObject( 'ADODB.Connection' );
conn.Open( 'Provider=SQLOLEDB;Integrated Security=SSPI;Server=mt;Database=test;Integrated Security=SSPI' );
rs = new ActiveXObject( 'ADODB.Recordset' );
//sqlQuery = "SELECT [id],[fullname] FROM persons WHERE [id] IN (5802824683954111059,1615647673927737)";
sqlQuery = "IF OBJECT_ID('tempdb..#temp_table_1') IS NOT NULL\r\n\
TRUNCATE TABLE #temp_table_1\r\n\
ELSE\r\n\
CREATE TABLE #temp_table_1 ( [key] bigint primary key );\r\n\
INSERT INTO #temp_table_1 ([key]) VALUES (5802824683954111059),(1615647673927737);\r\n\
SELECT [id],[fullname] FROM persons WHERE [id] IN (SELECT [key] FROM #temp_table_1)";
rs.Open( sqlQuery, conn );
while ( ! rs.EOF )
{
alert( rs.Fields( 'fullname' ) );
rs.MoveNext();
}

Try either adding SET NOCOUNT ON to your T-SQL batch or invoke the Recordset.NextResult method after processing the results of the SELECT query.
SET NOCOUNT ON instructs SQL Server suppress DONE_IN_PROC TDS protocol messages (row counts), which classic ADO has the nasty habit of returning as closed empty recordsets with no columns. Failure to consume these with NextResult will prevent the entire T-SQL batch from running to completion.
It's a good practice to invoke NextResult until the method returns False as that will ensure the entire batch runs to completion regardless of the SET NOCOUNT session setting. Note that this technique also applies to other client APIs (ADO.NET, JDBC, etc), although the exact method to consume results differ as well as the symptoms (partial result sets, undetected exceptions).

Related

Exception calling "ExecuteNonQuery"

When I attempt to insert new data in database via PowerShell, it works.
However, if the data is already in database, I get exception.
Someone had similar problem, Catch exception calling "ExecuteNonQuery", but I believe I am using the correct SQL statement in my PowerShell code, where I say SELECT 1
$SQL_insert = "BEGIN
IF NOT EXISTS (SELECT 1
FROM [dbo].[Group_Stats]
WHERE Date_of_Record = CAST(GETDATE() AS DATE)
AND [Group] = '$group')
BEGIN
INSERT INTO [dbo].[Group_Stats] ([Date_of_Record], [Group], [Windows_SEP_11],[Mac_SEP_11],[Windows_SEP_12],[Mac_SEP_12])
VALUES (CAST(GETDATE() AS DATE), REPLACE ('$group', 'My Company\', ''), $win_sep_11, $mac_sep_11, $win_sep_12, $mac_sep_12)
END
END"
Exception
Exception calling "ExecuteNonQuery" with "0" argument(s): "Violation of PRIMARY KEY constraint 'PK_Group_Stats'. Cannot insert duplicate key in object 'dbo.Group_Stats'.
And this is the database
Thanks
You are querying for the untrimmed group name, but when you insert, you call REPLACE() to trim "My Company" off $group. You should instead trim the $group first, then query and insert without calling REPLACE().

list records from sql server UDF that returns Table in classic asp

I wrote my first sql Server returning table UDF since thought was better than using a SP
but, while can easily retrieve the result from sql server.. I can't get result calling it from classic ASP ADO
UDF is as follows:
CREATE FUNCTION dbo.udf_Alert
(
#I nvarchar (30),
#L nvarchar (10)
)
RETURNS TABLE AS RETURN
(
SELECT a.Message, a.Height, a.backgroundColor, a.isFree
from Advice a
join ActiveMessages b on b.MessageID=a.MessageID
join Items i on b.ItemID=i.ItemID
join Sellers s on i.UserID=s.UserID
join Users u on u.UID=s.UID
WHERE
(i.ItemID=#I and a.Active='1' and b.Active='1' and i.active='1' and i.Show='1' and CHARINDEX('ALERT',u.Modules)>0
and a.ValidFrom<GETDATE() and a.ValidTo>GETDATE() and u.PaidUntil>GETDATE() and charindex(#L,a.Languages)>-1 or charindex('all',a.Languages)>-1 )
UNION ALL
SELECT a.Message, a.Height, a.backgroundColor, a.isFree
FROM Advice a, Users u
WHERE u.isFree='1' and a.isFree='1' and (CHARINDEX(#L,a.Languages)>-1 or Charindex('all',a.Languages)>-1)
)
and I can easily execute from SSMS calling
Select * from dbo.udf_Alert('281F50246','fr')
But I have to embed into a classic ASP routine but I've not found the way to do it..
tried the SP method.. but I got error when try to set the parameters:
here what I tried:
sql="Select dbo.udf_Alert('xx','yy')"
dim cmdA
set cmdA = Server.CreateObject("ADODB.Command")
cmdA.ActiveConnection= cn
cmdA.CommandType=4
cmdA.CommandText=sql
cmda.Parameters.Append cmdA.CreateParameter("fd", nvarchar, adParamInput,30, itemID)
cmda.Parameters.Append cmdA.CreateParameter("fde", nvarchar, adParamInput,10, LanguageID)
' cmdA.Parameters("#I")=ItemID '<-----ERRROR HERE
' cmdA.Parameters("#L")=LanguageID
set rs=cmdA.Execute()
so I tried set Parametrs in other way.. but got same result:
ADODB.Command error '800a0bb9'
Arguments are of the wrong type, are out of acceptable range, or are in conflict with one another.
Can suggest some advice?
Thanks
Sergio
We tend to create stored procedure "wrappers" for UDFs on a one-to-one basis which can be called directly from web code. For your example, this might look like
CREATE PROCEDURE [dbo].[pr_Alert]
#I nvarchar (30),
#L nvarchar (10)
AS
SELECT * FROM testDb.dbo.udf_Alert(#I, #L)
RETURN
Your commandText would then be simply the name of the wrapper SP, "pr_Alert".
Hope this helps.

Check for duplicate records in sql database using vb.net

Assuming my table consist of two columns ID and Name.
And assume I have my stored procedure working on vb.net that inserts rows into the database.
But my system needs to check if an ID entered in a textbox already exists in the database when ADD button is click.
CREATE PROCEDURE AddOfficeEquipmentProfile
(
#OE_ID varchar(11),
#OE_Category char(3) =NULL,
#OE_SubCategory char(3)= NULL,
#OE_Name varchar(35)=NULL,
#OE_User varchar(35)=NULL,
#OE_Brand varchar(15)=NULL,
#OE_Model varchar(35)=NULL,
#OE_Specs varchar(1000)=NULL,
#OE_SerialNo varchar(35)=NULL,
#OE_PropertyNo varchar(35)=NULL,
#OE_MacAddress varchar(100)=NULL,
#OE_Static_IP varchar(15)=NULL,
#OE_Vendor varchar(35)=NULL,
#OE_PurchaseDate smalldatetime,
#OE_WarrantyInclusiveYear int=NULL,
#OE_WarrantyStatus char(2)= NULL,
#OE_Status varchar(15)=NULL,
#OE_Dept_Code char(3)= NULL,
#OE_Location_Code char(8)= NULL,
#OE_Remarks varchar(1000)= NULL
)
AS
INSERT INTO tblOfficeEquipmentProfile (OE_ID, OE_Category, OE_SubCategory, OE_Name, OE_User, OE_Brand, OE_Model, OE_Specs, OE_SerialNo,
OE_PropertyNo, OE_MacAddress, OE_Static_IP, OE_Vendor, OE_PurchaseDate, OE_WarrantyInclusiveYear, OE_WarrantyStatus, OE_Status, OE_Dept_Code,
OE_Location_Code, OE_Remarks )
VALUES (#OE_ID, #OE_Category, #OE_SubCategory, #OE_Name, #OE_User, #OE_Brand, #OE_Model,
#OE_Specs, #OE_SerialNo, #OE_PropertyNo, #OE_MacAddress, #OE_Static_IP, #OE_Vendor, #OE_PurchaseDate, #OE_WarrantyInclusiveYear, #OE_WarrantyStatus,
#OE_Status, #OE_Dept_Code, #OE_Location_Code, #OE_Remarks)
GO
few things you can do
make ID column as primary key, when insert you will get exception if duplicated
You can use auto increment ID, then you don't need to check ID exit or not. database will handle that
If you can't do above, run select statement or stored procedure to check whether id exist or not.
If this is for SQL Server and you're using a stored procedure - just try something like this:
CREATE PROCEDURE AddOfficeEquipmentProfile
(
#OE_ID varchar(11),
..... all your other parameters here.....
)
AS
IF NOT EXISTS (SELECT * FROM dbo.tblOfficeEquipmentProfile WHERE OE_ID = #OE_ID)
INSERT INTO dbo.tblOfficeEquipmentProfile(.... list of columns.....)
VALUES (......list of values................)
Assuming that OE_ID is your primary key and will be unique. Just check if that #OE_ID doesn't exist yet, and if it doesn't - insert the data. If it exists - don't do anything.
Building on the answer from #marc_s. In order to show a message to the user in case there already is a row in the database with the same id, you can check the number of affected rows from the query execution result.
This assumes that the stored procedure only inserts the row if the id is not present and does not emit any errors/exceptions.
Using ADO.NET (with an existing command executing the stored procedure):
Dim affectedRows as Integer = command.ExecuteNonQuery()
If affectedRows = 0 Then
'Handle the error here
MessageBox.Show("There is already a Profile with the supplied id")
Else
'Insert was made
End If
Check the following article to create a SP finding duplicate rows in any table:
http://www.codeproject.com/Articles/157977/Remove-Duplicate-Rows-from-a-Table-in-SQL-Server

Why is the same value on IDENTITY column being returned to two inserters?

My program is inserting into a table like this...
CREATE TABLE [DBO].[MYTABLE] (
ID [INT] IDENTITY (1,1),
DATE_TIME [DATETIME],
NOTES [VARCHAR] (100))
using the following code...
database_connection = New ADODB.Connection
' ...code to connect...
database_connection.IsolationLevel = ADODB.IsolationLevelEnum.adXactSerializable
database_connection.BeginTrans
command_string = "INSERT INTO [MySchema].[dbo].[MyTable] (NOTES) VALUES ('sometext')"
database_connection.Execute(command_string)
command_string = "SELECT MAX([id]) as max_id FROM [MySchema].[dbo].[MyTable]"
Dim record_set As ADODB.Recordset = New ADODB.Recordset
record_set.CursorLocation = ADODB.CursorLocationEnum.adUseClient
record_set.Open(command_string, database_connection, ADODB.CursorTypeEnum.adOpenStatic, , ADODB.CommandTypeEnum.adCmdText)
record_set.MoveLast
new_id = CInt(record_set.Fields("id").Value)
database_connection.CommitTrans
Occasionally this code is executed by two different programs simultaneously (close enough in time that the date_time values are identical), and although there are two rows visible in MyTable, both programs are running with the same new_id.
Although I appreciate I should probably be using SCOPE_IDENTITY here (and I'll try it in a minute), I was under the impression that the SERIALIZABLE transaction would prevent this from happening. Does anyone know why this duplication is occurring, and if SCOPE_INDENTITY will fix the problem?
You definitely need to use SCOPE_IDENTITY, but in the same query as INSERT.
When 2 clients insert data in a same table at same time -> the second query will return Max(id) of 2nd inserted value.
INSERT INTO [MySchema].[dbo].[MyTable] (NOTES) VALUES ('sometext')
SELECT SCOPE_IDENTITY()
the easiest way would be opening the recordset with following command, without previous execute
INSERT INTO [MySchema].[dbo].[MyTable] (NOTES)
OUTPUT inserted.id
VALUES ('sometext')

Why triggers try to insert NULL value when using a field from 'inserted' table?

I have to sync changes done in MSSQL with a remote MySQL database. The changes to be synced are adding invoices and users to the system. The remote server is not expected to be always reachable so I'm trying to set up a kind of log table for storing changes done in MSSQL.
Here is a fully working trigger for that:
CREATE TRIGGER [dbo].[dokument_insert]
ON [dbo].[dokument]
AFTER INSERT
AS
BEGIN
SET NOCOUNT ON;
INSERT INTO [bcg_ekodu].[dbo].[sync_stack] (event,sql, table_name, import_priority)
SELECT
'INSERT',
'INSERT INTO bills SET
date = "'+CONVERT(VARCHAR(19),dok_kuup,120)+'",
total = "'+CAST(kokkusum AS nvarchar)+'",
number = "'+RTRIM(dok_nr)+'",
created = "'+CONVERT(VARCHAR(19),savetime,120)+'",
rounded = "'+CAST(ymardus AS nvarchar)+'",
currency = "'+CAST(valuuta AS nvarchar)+'",
due_date = "'+CONVERT(VARCHAR(19),tasupaev,120)+'",
pk_joosep = "'+CAST(dok_kood AS nvarchar)+'",
joosep_hankija = "'+CAST(hankija AS nvarchar)+'";
UPDATE
bills, users, companies
SET
bills.user_id = users.id,
bills.imported = NOW()
WHERE
bills.imported IS NULL
AND companies.id = users.company_id
AND companies.pk_joosep = 10
AND bills.user_id = users.pk_joosep',
'bills',
'200'
FROM inserted
END
It inserts a row into 'sync_stack' table every time a row is inserted to 'dokument' table. The 'sql' column will contain an SQL to create the same kind of row in another (MySQL) database.
But this trigger is not working:
CREATE TRIGGER [dbo].[klient_insert]
ON [dbo].[klient]
AFTER INSERT
AS
BEGIN
SET NOCOUNT ON;
INSERT INTO [bcg_ekodu].[dbo].[sync_stack] (event,sql, table_name, import_priority)
SELECT
'INSERT',
'INSERT INTO users SET
username =10'+CAST(kl_kood as nvarchar)+',
password = NULL,
name ="'+LTRIM(RTRIM(kl_nimi))+'",
email ="'+CAST(LTRIM(RTRIM(kl_email)) as nvarchar)+'",
reference_no ="'+CAST(LTRIM(RTRIM(kl_viide)) as nvarchar)+'",
phone ="'+CAST(LTRIM(RTRIM(kl_tel1)) as nvarchar)+'",
logins ="'+CAST(0 as nvarchar)+'",
last_login = NULL,
created ="'+CONVERT(VARCHAR(19),savetime,120)+'",
updated = NULL,
deleted ="0",
address ="'+CAST(LTRIM(RTRIM(kl_aadr1)) as nvarchar)+'",
pk_joosep ="'+CAST(kl_kood as nvarchar)+'"',
'users',
'210'
FROM inserted
END
While the execution of the above SQL to create that trigger completes just fine, when I try to insert some rows to the 'triggered' table, I get the following error:
No row was updated.
The data in row 175 was not committed.
Error Source: .Net SqlClient Data Provider.
Error Message: Cannot insert the value NULL into column 'sql', table 'mydb.dbo.sync_stack'; column does not allow nulls. INSERT fails.
The statement has been terminated.
Correct the errors and retry or press ESC to cancel the change(s).
If I delete this trigger, this error does not occur.
If I insert just plain text for 'sql' column, it works as expected.
If I use any field from the inserted row, even just a text field, it fails again.
If I allow NULL values in 'sql' column, inserting rows succeeds but I get a NULL value in 'sql' column.
How to make the second trigger work as expected, too?
I suspect that at least one of the values from inserted that you are concatenating into your SQL statement is NULL. You can circumvent this by using COALESCE, e.g.
username =10'+COALESCE(CAST(kl_kood as nvarchar), '')+',
Of course you shouldn't be declaring nvarchar without specifying a length, right?
Bad habits to kick : declaring VARCHAR without (length)
Concatenating any value to NULL is NULL:
select 'test' + NULL
Results in null, you should use something like that for your columns:
select isnull(column, '')
This would result in an empty string.

Resources