I get a syntax error with this TSQL:
if exists ( exec('select * from mytable') at LinkedOracleServer ) begin
print 'rows exist'
end
I can't use INSERT EXEC into a temp table because the calling proc also uses INSERT EXEC and I get the error "An INSERT EXEC statement cannot be nested."
Is there another way to test for existing rows on a linked server?
It is just a suggestion. Can't you try like this
select #recod_count = count(*) from LinkedOracleServer.mytable
if #recod_count>1 begin
end
Related
I had written and successfully created a stored procedure by the query
CREATE PROCEDURE [dbo].[Table_Load]
AS
BEGIN
SET NOCOUNT ON
DECLARE #Row_Count_Inserted BIGINT
DROP TABLE IF EXISTS DBB.dbo.Table;
SELECT *
INTO DBB.dbo.Table
FROM
(SELECT *
FROM DBB.dbo.customer_table) y
SET #Row_Count_Inserted = ##RowCount
SELECT #Row_Count_Inserted Row_Count_Inserted
END
This shows that the stored procedure is created and is present in the database. But when I query the table 'Table' using
SELECT * FROM DBB.dbo.Table
I get an error
Invalid object name
How can I solve this issue? I have refreshed the database as well but it does not work.
Here is your procedure greatly simplified to remove a lot of extra code. Assuming you have the table customer_table this will work just fine.
CREATE or alter PROCEDURE [dbo].[Table_Load] As
Begin
SET NOCOUNT ON
drop table If Exists dbo.MyTable;
Select *
into MyTable
from customer_table
select Row_Count_Inserted = ##RowCount
End
GO
exec Table_Load
GO
select * from MyTable
I have a SQL Server function and I want to convert to Oracle function.
This is my function:
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE FUNCTION [dbo].[SUB_FOLDERS]( #GRUP_GRUP NVARCHAR(30),#STATU NVARCHAR(30))
RETURNS #Strings TABLE (GRUP_KODU nvarchar(100))
AS
BEGIN
DECLARE #sonuc nvarchar(4000)
DECLARE merge_cursor CURSOR FAST_FORWARD FOR WITH temptab(id)
AS
( SELECT root.GRUP_KODU FROM GRUP_TABLE root WHERE GRUP_KODU=#GRUP_GRUP AND DURUM=#STATU
UNION ALL
SELECT sub.GRUP_KODU FROM GRUP_TABLE sub, temptab super WHERE sub.GRUP_GRUP = super.id )
SELECT * FROM temptab OPEN merge_cursor WHILE ##FETCH_STATUS = 0
BEGIN
SET #sonuc=''
FETCH NEXT FROM merge_cursor INTO #sonuc if (#sonuc<>'')
INSERT INTO #Strings VALUES (#sonuc)
END
RETURN
END
GO
When I run it on SQL Server it works properly but on Oracle, it gives error.
Error report:
SQL Command: functıon SUB_FOLDERS
Failed: ORA-24344: success with compilation error
24344. 00000 - "success with compilation error"
*Cause: A sql/plsql compilation error occurred.
*Action: Return OCI_SUCCESS_WITH_INFO along with the error code
Thanks in advance.
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 have to develop a WinCC Visual Basic Script management application. In this application I read an XML archive, and after that I put the information in a SQL database through a SQL INSERT query.
My problem is that I don't know how to do the error handling to view the SQL errors in VBScript MsgBox for example.
Activating the error handling with On Error Resume Next and after the evaluation of these errors with If Err.Number <> 0 Then ... the errors produced in SQL Server don't appear in VBScript.
If you want to get SQL Server error, You can use stored procedure with transaction to insert data into table:
create procedure dbo.InsertTable (
#param1 nvarchar(80)
,#param2 nvarchar(80)
,#error_text nvarchar(400) output)
as
begin
begin tran
begin try
insert into YourTable (column1, column2)
values (#param1, #param2)
end try
begin catch
set #error_text = error_message()
rollback
return
end catch
commit
end
Now You will get eventually error from the output parameter #error_text
declare #error_text nvarchar(400)
exec dbo.InsertTable 'Value1','Value2', #error_text output
select #error_text
I have the below dynamic query run in SQL Server, connecting to an OLAP server using a linked server, which returns a table as a result.
SET #nSQL = EXECUTE ('SELECT non empty {
[Coded Season].[Coded Season].[Coded Season] *
[Season].[Season].[Season] *
[Product].[Subclass].[Subclass] *
[Product].[Subclass Id].[Subclass Id]
} ON ROWS,{
[Measures].[Pl No of Range Opts]
} ON COLUMNS
FROM RP_C0') AT AS_T_RP_5900_Admin
I am executing it in SQL Server like this:
exec sp_executesql #nSQL;
It returns a table of values. Now I want to insert the data into a temporary table. I have tried the below code, but its not working.
INSERT INTO ##Subclass_Season_AS
exec sp_executesql #nSQL;
Also tried,
set #strNewQuery ='SELECT '+#nSQL+' INTO ##temptablename '
exec #strNewQuery
Could you please help on this? Thanks!
You may want to try to put the INTO statement in your dynamic query.
SET #nSQL = EXECUTE ('SELECT non empty {
[Coded Season].[Coded Season].[Coded Season] *
[Season].[Season].[Season] *
[Product].[Subclass].[Subclass] *
[Product].[Subclass Id].[Subclass Id]
} ON ROWS,{
[Measures].[Pl No of Range Opts]
} ON COLUMNS
INTO ##temptablename
FROM RP_C0') AT AS_T_RP_5900_Admin