Store JSON Data into SQL [duplicate] - sql

Hi I created a stored procedure that uses OPEN JSON and insert data into a table.
The problem is when I run the stored procedure it shows an error.
I am using SQL server 2016 (SQl Server 13.0.4446.0). I am not getting the same issue when using using sql server 13.0.1742.0
CREATE PROCEDURE [dbo].Test2--'[{"FileId":1,"DataRow":"3000926900"}]'
(
#data varchar(max)
)
AS
BEGIN
create table #Temp
(
FileId bigint,
DataRow nvarchar(max),
DateLoaded DateTime
)
INSERT INTO [dbo].#Temp
SELECT * FROM OPENJSON(#data)
WITH (FileId bigint,
DataRow nvarchar(max),
DateLoaded DateTime)
select * from #temp
END
Error:
If this statement is a common table expression, an xmlnamespaces clause or a change tracking context clause, the previous statement must be terminated with a semicolon.

Check your database compatibility level. OPENJSON is new with SQL Server 2016, and if your compatibility level is set "SQL Server 2014 (120)" or less the OPENJSON function will not be correctly recognized or executed. See the MSDN docs at https://learn.microsoft.com/en-us/sql/t-sql/functions/openjson-transact-sql .

Related

Need to get data from another server(AS400) using linked server based on data from temp table(SQL Server) as dynamic sql query

Need to get data from another server(AS400) using linked server based on data from temp table(SQL Server) as dynamic sql query. How do I pass the values from temp table to linked server where condition in dynamic query.
Here is the code.
Declare #itemNo VARCHAR(100);
SELECT PRJNO into #tbltemp
FROM OPENQUERY([XXXX], 'SELECT PRJNO FROM XXXX WHERE ITEMnO IN
( '''+#itemNo+''')

How to create a view where data resides in a view on remote Azure SQL Server

I'm trying to create a view of data that resides in remote Azure SQL Server. I can't seem to create a table or temp table in the view to store the results of the sp_exeucte_remote call because that is not allowed so I tried to use a function, but then I get an error complaining about the following error based on the provided function definition.
Invalid use of a side-effecting operator 'INSERT EXEC' within a function
CREATE OR ALTER FUNCTION [dbo].[fn_Test]()
RETURNS #Results TABLE
(
[ID] INT,
[$ShardName] VARCHAR(500)
)
AS
BEGIN
INSERT INTO #Results
EXEC sp_execute_remote N'MyExternalDatasource', N'SELECT 1'
RETURN;
END
How can one create a view of data that exists on a remote Azure SQL Server where that data also exists as a view? FYI - the server where I'm trying to create the view is also Azure SQL Server.
Why are you using a FUNCTION?
As per Microsoft Documentation you cannot call a Stored Procedure within a function.
Also, your remote execution is only returning 1 column and you haven't defined the destination column to insert to.
The fact that the remote object is a VIEW does not matter. If we assume that the VIEW on the remote database has columns also named ID and [$shardname] then why not just use something like:
CREATE TABLE #results ([ID] int, [$shardname] varchar(500))
INSERT #results ([ID], [$shardname])
EXEC sp_execute_remote N'ExternalSource', N'SELECT [ID], [$shardname] FROM RemoteTableName'

Import CSV file into SQL Server 2012 as SQL function

So I am basing my code on the Import to mapped columns question asked by another user.
Here is the code...
DECLARE #TempTable TABLE (Name nvarchar(max))
BULK INSERT #TempTable
FROM ‘C:\YourFilePath\file.csv’
WITH ( FIELDTERMINATOR = ‘,’,
ROWTERMINATOR = ‘\n’
)
INSERT INTO TABLE ([Name], [TypeId])
Select Name,'99E05902-1F68-4B1A-BC66-A143BFF19E37' from #TempTable
Do I put this code into a stored procedure or a function to run it from my ASP script?
Is your TypeID a constant at the last line?
You can do either SP or TableFunction. But since TableFunction is depandant on the version of SQL Server you have, it might make sense to use SP.

Insert results of execute(#QueryString) into a table while adding an extra column

I am writing a stored procedure that dynamically creates a SQL string, #SQLQuery. After I create this query, I need to execute the query and insert it into a table in the database while adding another column that specifies a unique ID for this particular insert. (Context: It is possible in this application that multiple groupings of data will be inserted into this table and I need to be able to differientiate between groupings at a later date. )
This issue is similar to this question except I am using Microsoft SQL Server 2008 instead of mysql. I have tried the solution there:
INSERT INTO data_table_name
EXECUTE(#SQLQuery), #SomeID
but MS SQL Server 2008 doesn't like that syntax.
Any ideas on how to do this in SQL Server 2008?
You can store query result in table variable and then read from that with extra column and write to final table
DECLARE #temp table (col1 int, col2 varchar(10), ....)
INSERT INTO #temp
EXEC(#SQLQuery)
INSERT INTO data_table_name
SELECT *, #SomeID FROM #temp
You can also append #SomeID in your dynamic sql string.
Example:
SET #SQLQuery = 'SELECT *,' + #SomeID + ' FROM ' + #tableNameVar
and then do this
INSERT INTO data_table_name
EXECUTE(#SQLQuery)
Since you mentioned you are doing this in a Stored Procedure, what I would suggest is for you to:
Execute the #SQLQuery first;
Upon successful execution of the #SQLQuery, insert the #SQLQuery to the table with the unique ID.
i.e.
EXEC sp_executesql #SQLQuery, #Param
IF ##ERROR <> 0
BEGIN
INSERT INTO TableA(Query)
VALUES(#SQLQuery)
END
You're better off designing TableA such that the ID will be an Identity so that a unique sequential ID is generated when you insert a record into that table.

SQL Server - Pass image to stored procedure, invalid local var type

I have two tables that contain document content: one for temporary staging, other for permanent storage. The content is stored as type image (cannot change this since it's current functionality).
I need a stored procedure that does the following:
Pass in a TempDocumentID that exists in temp document table.
With that TempDocumentID, select image content from temp document table.
Exec existing stored procedure that takes an image parameter to insert into permanent document table.
My problem is two-fold:
I can't declare a local variable of type 'image' to fill from the select statement of temp table. It throws error 'The text, ntext, and image data types are invalid for local variables.'
I don't know of a way to exec stored proc with direct results from select statement of temp table.
Here is my SQL Fiddle example: http://sqlfiddle.com/#!3/09384/5
Thanks,
Greg
Try this, it doesn't get an error in SQL Fiddle. I believe it will pass the result from the sub-query:
CREATE PROCEDURE MoveDocumentFromTemp
(
#TempDocumentID numeric(18,0)
)
AS
BEGIN
EXEC InsertDocumentContentFinal (SELECT TempContent
FROM DocumentContentTemp (NOLOCK)
WHERE TempDocumentID = #TempDocumentID)
END
You should be able to use VARBINARY(MAX) with SQL Server 2005 and later.
CREATE PROCEDURE MoveDocumentFromTemp
(
#TempDocumentID numeric(18,0)
)
AS
BEGIN
DECLARE #ContentToMove varbinary(max)
SELECT #ContentToMove = cast(TempContent as varbinary(max))
FROM DocumentContentTemp (NOLOCK)
WHERE TempDocumentID = #TempDocumentID
EXEC InsertDocumentContentFinal #ContentToMove
END
GO
For SQL Server 2000, you'll just have to include the INSERT code from MoveDocumentFromTemp directly into your wrapper stored procedure.