I have the following stored procedure in SQL Server:
CREATE PROCEDURE [dbo].[EmptyStagingTableBySupplierNr]
#suppliernumber NVARCHAR(50)
AS
BEGIN
-- routine body goes here, e.g.
-- SELECT 'Navicat for SQL Server'
INSERT INTO proclog (description)
VALUES (#suppliernumber);
DELETE FROM [dbo].[productassortment_staging]
WHERE [LieferantenNr] = #suppliernumber;
INSERT INTO proclog (description)
VALUES ('test');
END
If I execute the stored procedure directly all parts (insert, delete) work fine.
But if I execute the stored procedure from a third party software - only the insert parts are working.
I simplified the stored procedure:
CREATE PROCEDURE [dbo].[EmptyStagingTableBySupplierNr]
#suppliernumber NVARCHAR(50)
AS
BEGIN
-- routine body goes here, e.g.
-- SELECT 'Navicat for SQL Server'
INSERT INTO proclog (description)
VALUES (#suppliernumber);
DELETE FROM [dbo].[productassortment_staging]
WHERE [LieferantenNr] = '123';
INSERT INTO proclog (description)
VALUES ('test');
END
The same result: it works, if I execute it directly, but from the third party software only the insert parts are working.
I have changed the used credentials to the admin account. No change - so
a problem with insufficient rights could be excluded.
Any ideas how I can solve the problem?
Related
How can I run the stored procedure when there is an insert operation in SQL Server (without trigger), that is, when data is entered into the table?
CREATE TRIGGER Mail_Send
ON [Oral_Overprint_2].[dbo].[table]
AFTER INSERT
AS
BEGIN
EXEC Mail_Table_Procedure
END
I have created a SQL Server stored procedure which contains insert and update statements and log table. When I execute the stored procedure locally using
EXEC stored_procedure_name param1
it works perfectly fine and logs entry is inserted into the log table; but when the same stored procedure is called from another server using the same SQL Server user, the stored procedure is called and correct output/response is going to calling server but insert and update statements are not executed and no logs are inserted.
Again, when I call the stored procedure locally, everything works. When I checked the entries in log table (with Id column as identity(1,1)), there are 2 entries with id 1 and 3.
I think entry with Id = 2 is inserted and then rollback as it called from another server.
I don't understand the different behavior. Please help.
Thanks in advance
ALTER PROCEDURE [dbo].[temp_procedure]
(#aid VARCHAR(MAX))
AS
BEGIN
SET NOCOUNT ON;
DECLARE #RETURN_CODE INT = -2;
DECLARE #RETURN_MESSAGE VARCHAR(MAX) = 'ENTITY NOT FOUND';
-- Check IF exist and Update
IF EXISTS (SELECT 1 FROM temp WHERE c = #aid)
BEGIN
UPDATE temp
SET a = 0, b = 0,
WHERE c = #aid;
SET #RETURN_CODE = 0;
SET #RETURN_MESSAGE = 'SUCCESS';
END
-- Log action and result in logging table for particular a_id
-- also has id column as identity
INSERT INTO [dbo].[Logs] ([A_Id], [Created_Date], [Return_Code], [Return_Message])
VALUES (#aid, GETDATE(), #RETURN_CODE, #RETURN_MESSAGE)
SELECT #RETURN_CODE AS 'RETURN_CODE', #RETURN_MESSAGE AS 'RETURN_MESSAGE';
END
I am trying to insert the data of a stored procedure into a temp table like below
CREATE TABLE #CustomTable3HTML
(
ItemId varchar(30),
ItemId1 varchar(30)
)
INSERT INTO #CustomTable3HTML
EXEC SalesDeals.dbo.prGetDealProposalDetail 17100102, 1
but I am getting this error
Msg 8164, Level 16, State 1, Procedure prGetDealProposalDetail, Line 138 [Batch Start Line 1]
An INSERT EXEC statement cannot be nested.
I figured this is because the stored procedure already has an insert into clause defined and I found out that it can be used only once in the calling chain.
So I started looking for other options and found out about OpenRowSet which I am using as below
SELECT *
INTO #CustomTable3HTML
FROM OPENROWSET('SQLOLEDB','Server=Demo\Demo;Trusted_Connection=Yes;Database=SalesDeals',
'SET NOCOUNT ON;SET FMTONLY OFF;EXEC SalesDeals.dbo.prGetDealProposalDetail 17100102,1')
I am getting an error when I run this SQL command
Access to the remote server is denied because no login-mapping exists.
It works fine when I use a higher level account like sysadmin but fails with the other account which is a normal db owner on the database where I am running this SQL.
There is work around of this. It's not beautiful, but it will work.
In our outer query define a table:
CREATE TABLE #CustomTable3HTML
(
ItemId varchar(30),
ItemId1 varchar(30)
)
Change the procedure adding the following code at the end:
IF OBJECT_ID('tempdb..#CustomTable3HTML')
BEGIN
INSERT INTO #CustomTable3HTML
SELECT ....
END
ELSE
BEGIN
SELECT ....
END
After executing the stored procedure you will have the data in table.
I'm trying to run a stored procedure that creates a local table - #table1
The stored procedure is supposed to look for values and create the table and insert the values into it...
INSERT INTO #table1
I execute the stored procedure and it shows that 1 row() affected, however, I am unable to find this table in the list of my tables. Why am I not able to see it or access it?
EDIT: I'm running the stored procedure inside SQL Server against a database. At the end of the stored procedure, the last line is:
Select * from #table1
Thanks.
The #table is a local temp table. It does not exist as a permanent table that you can look for outside the scope of the stored proc. Once the stored proc is run, the temp table is dropped because it is no longer in scope. Temp tables are stored temporarily in the tempdb database but with a different name because two people running the stored procedure at the same time would each have a table that can be referenced in the proc as #table but it would be two separate tables in the tempdb.
Now if what you are doing is looking to see what is in #table at a point in the stored proc in order to troubleshoot the proc, then you need to set thing up in the proc so that you can see the results at different stages or when you hit a certain state such as an error.
This could be something like adding a #debug variable to the proc so that when you are in debug mode, you can select the results to the screen when you are running something like:
CREATE PROC test_proc (#Id INT, #debug BIT = 0)
AS
CREATE TABLE #temp(id INT)
INSERT INTO #temp
VALUES (#Id), (1), (2)
IF #debug = 1
BEGIN
SELECT * FROM #temp
END
UPDATE #temp
SET Id = id-1
IF #debug = 1
BEGIN
SELECT * FROM #temp
END
GO
You would then execute the proc without debugging as so (note that since I am not returning something or inserting to permanent tables, this proc will insert to #temp but you can't see anything. I just didn't want to get complicated here, the steps of the proc will vary depending on what you want to do, the concept I am trying to show is how to use the debug variable):
EXEC test_proc #Id= 5
and with debugging as
EXEC test_proc #Id= 5, #debug= 1
Or it might involved using a table variable instead (because they don't get rolled back on error) and then inserting the data from that table variable into a logging table after the rollback occurs in the Catch block, so that you can see the values at the time the error occurred.
Without knowing more about why you are looking for #temp and what the data means and is used for, it is hard to say what you need to do.
Did you tried refreshing the tables after exceuting Stored procedure
I have two different SQL 2008 servers, I don't have permission to create a linked server in any of them.
i created a trigger on server1.table1 to insert the same record to the remote server server2.table1 using OPENROWSET
i created a stored procedure to insert this record, and i have no issue when i execute the stored procedure. it insert the recored into the remote server.
the problem is when i call the stored procedure from trigger, i get an error message.
can anyone help me please to solve this issue
Stored Procedure:
USE [DB1]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
alter PROCEDURE [dbo].[InsertIntoRemoteTable]
#Description nvarchar(50)
AS
insert into
OPENROWSET(
'SQLNCLI', 'Server=Server2;UID=MySRV2user;PWD=MySRV2Password',
'SELECT Description FROM [RemoteDB].[dbo].[Table_1]')
SELECT #Description
Trigger:
ALTER TRIGGER [dbo].[InsertTable1]
ON [DB1].[dbo].[Table_1]
for insert
AS
BEGIN
SET NOCOUNT ON;
DECLARE #Desc nvarchar(50)
Select #Desc = i.Descr from INSERTED i;
EXEC InsertIntoRemoteTable #Desc
END
When i try to insert a new description value in the table i got the following error message:
"No row was updated
the data in row 1 was not committed
Error source .Net SqlClient Data provider.
Error Message: the operation could not be performed because OLE DB
provider "SQLNCLI10" for linked server "(null)" returned message "The partner transaction manager has disabled its support for remote/network transaction.".
correct the errors entry or press ESC to cancel the change(s).
can anyone help please on this
thanks
I think you have over complicated this. There is really no need for a stored procedure here to do this. You can do this insert into another database directly in your trigger with a simple insert statement. This removes a ton of complexity and it is set based.
ALTER TRIGGER [dbo].[InsertTable1]
ON [DB1].[dbo].[Table_1]
for insert
AS
BEGIN
SET NOCOUNT ON;
INSERT INTO RemoteDB].[dbo].[Table_1](Description)
Select i.Descr
from INSERTED i;
END