I have an H2 database (http://www.h2database.com) and I'd like to insert a file into a BLOB field via a plain simple sql script (to populate a test database for instance). I know how to do that via the code but I cannot find how to do the sql script itself.
I tried to pass the path, i.e.
INSERT INTO mytable (id,name,file) VALUES(1,'file.xml',/my/local/path/file.xml);
but this fails.
Within the code (java for instance), it's easy to create a File object and pass that in, but directly from a sql script, I'm stuck ...
Any idea?
For testing, you can insert literal hex bytes or use the RAWTOHEX(string) function, as shown below.
create table a(id integer, item blob);
insert into a values(1,'54455354');
insert into a values(2, RAWTOHEX('Test'));
select UTF8TOSTRING(item) from a;
TEST
Test
Addendum: For loading BLOB fields from a file, FILE_READ(fileNameString) may be a useful alternative.
insert into a values(3, FILE_READ('file.dat'));
Not h2database, but may help; https://blog.jerrynixon.com/2009/03/tsql-to-insert-imageblog.html
Example code from the linked blog article, should the link break again:
CREATE TABLE MyTable
(id int, image varbinary(max))
INSERT INTO MyTable
SELECT 1
,(SELECT * FROM OPENROWSET(
BULK 'C:\file.bmp', SINGLE_BLOB) as x )
Related
I'm using SQL Server 2019 - v15.0.4123.1.
This is the sample query (from this question):
DECLARE #tbl TABLE (ID INT IDENTITY PRIMARY KEY, city VARCHAR(30))
INSERT INTO #tbl (city)
VALUES ('Miami'), ('Orlando');
SELECT
'SIN_OPE' AS [#cod_1],
'08' AS [#cod_2],
'12' AS [#num_reg],
'yyyyyyyyyyyyyyyyyyy.xsd' AS [#xsi:noNamespaceSchemaLocation],
(SELECT *
FROM #tbl
FOR XML PATH('r'), TYPE)
FOR XML PATH('root'), TYPE, ELEMENTS XSINIL;
The result of the query is:
I just want to save/export this file as an XML file, automatically (with a stored procedure) to a folder in my PC. I've found many possible solutions but they are pretty old and doesn't work with my environment. I still can't understand if it's possible to export XML from SQL Server. I would like to avoid the manual passage "Query - Results to - Results to file"
Any advice?
Thank you
On you SQL Server Management Tool go to Query --> Result --> Result to file and then execute your query.
once you execute the query it will open a window & give you an option to save the file, there you can save it as xml.
how can we convert the result set of a procedure into an XML without creating a temporary table since the result set may vary in their structure based on the inputs ? the scenario must work in both on premise and azure hosted SQL server databases.
CREATE PROCEDURE SPSample(#Scope VARCHAR(2))
AS
BEGIN
IF(#Scope='A')
SELECT 1 AS ID,2 AS NAME
IF(#Scope='B')
SELECT 1 AS CustomerID,'Sample#sample.com' AS Email,'121314' AS ContactNumber
END
I believe you are asking to use FOR XML or FOR XML AUTO:
https://learn.microsoft.com/en-us/sql/relational-databases/xml/for-xml-sql-server?view=sql-server-2017
create table mytable(col1 int, col2 nvarchar(20), col3 money)
insert into mytable(col1, col2, col3) values (1, 'string', 123.45)
select * from mytable
for xml auto
Emits:
You can do this on any query result set:
select * from sys.objects
for xml auto
This will emit that structure similarly.
i need a way that the result set from a stored procedure directly converted to XML object without creating a table structure since the result set from procedure may vary
There's no way in TSQL to do that. TSQL can only convert to XML using a SELECT, not an EXECUTE. You could do this with a loopback linked server or CLR, but those are hacks, and not available in Azure SQL DB. You'll have to find another way. EG
CREATE PROCEDURE SPSample(#Scope VARCHAR(2), #ResultsAsXml bit = 0)
. . .
I am new bee in learning microsoft technologies.
I struck with an issue in sql server where I need your help.
Well, I have a XML file with below format,
please see it for your reference
<Permissions><Denied><User/><Roles/><Groups/></Denied><Allowed><Users/><Roles>admin,user,reader,writer,</Roles><Groups/></Allowed></Permissions>
In which I need to read Roles node values and insert those comma separated values as single row where I I will pass permissionid as a parameter in stored procedure.
here is the table columns (I need to insert single role for single row in test table based on transitionid)
create table test
(
empid int identity(1,1),
roles varchar(40),
transitionid int
)
You have two problems here: getting the data from the XML and splitting it.
If you're using SQL 2016 you're in luck - there's a new STRING_SPLIT function. You could use that like so:
declare #xml xml = '<Permissions><Denied><User/><Roles/><Groups/></Denied><Allowed><Users/><Roles>admin,user,reader,writer,</Roles><Groups/></Allowed></Permissions>';
declare #test table
(
empid int identity(1,1),
roles varchar(40),
transitionid int
)
INSERT #test (roles)
select b.value
FROM #xml.nodes('//Roles/text()')x(csv)
CROSS APPLY STRING_SPLIT(CAST(x.csv.query('.') AS VARCHAR(MAX)), ',')b
where b.value <> ''
select * from #test
Otherwise, you'll have to do something similar using a custom string splitting method, which you can find more about How do I split a string so I can access item x? or https://sqlperformance.com/2012/07/t-sql-queries/split-strings - basically, both require either writing a custom T-SQL function or CLR code that is imported into SQL Server. You could then use the same method as above (replacing STRING_SPLIT with the name of your custom string splitting function).
I have this table in a database.
create table #temp
(
name nvarchar(max)
)
insert into #temp
(
name
)
values
('ปภวรินทร์ เฉื่à¸à¸¢à¹„ธสง')
select * from #temp
When I am seeing this data in the website. the data is displaying as
ชญา สวัสดิ์โยธ
But when I am exporting this data to csv it is displaying as
ปภวรินทร์ เฉื่à¸à¸¢à¹„ธสง
I want to export the data to csc from sqlserver in the same way that shows in WEB.
How can i do that ?
Thanks in advance.
Try to add N before inserting your values. You can insert UNICODE characters.
insert into #temp
(
name
)
values
(N'ชญา สวัสดิ์โยธ')
This is either (a) an issue with your UTF target (e.g. you are targeting UTF-32 on export rather than UTF-8 / 16) or (b) the db you are using requires a symbolic string for inserting these characters-- sort of like how "??!" is a trigraph for "|".
CREATE TABLE updater
(
nzp_up SERIAL PRIMARY KEY,
version VARCHAR(50),
status INT,
report TEXT
);
INSERT INTO updater (version, status,report) values ('TestVersion' , 0,"123123123");
-617 SQL error: A blob data type must be supplied within this context.
Using a | (pipe) delimited file, you can use the LOAD command to insert values into blob & text data types. I had the same problem in the past - go to link in my comment
See my question: Consistent method of inserting TEXT column to Informix database using JDBC and ODBC
It seems that some tools like ODBC drivers can insert text as TEXT while others like JDBC drivers must use PreparedStatent or other techniques.
INSERT INTO updater (version, status,report)
values ('TestVersion' , 0,"123123123");
and
INSERT INTO updater (version, status,report)
values ('TestVersion' , 0,'123123123');
have the same effect in mySql.So lets try without double quotes in SQL.