Store/Fetch text with emoji Sql Server 2014 - sql-server

Please help me on it: I've a textbox in which user is able to type text like Hi, This is test of emoji ☢️ and I've to save this record in table. Also once I pull this record from table and showing to client end must show same along with emoji. I've created column type as nvarchar(450) with SQL server 2014 database. I'm sending this text using API to stored procedure.
Currently I'm passing text as Hi, This is test of emoji ☢️ then storing in DB as Hi, This is test of emoji ??
This is my stored proc:
ALTER proc [dbo].[SP_Save_Not_StatsTrans] #Not_Content nvarchar(max), #PublishedOn datetime
as
begin
insert into Tb_Notifications(ObjectUid, Not_Content,PublishedOn) values
(NEWID(),#Not_Content,#PublishedOn)
end

I would be interested to see your insert statement. Emojis are Unicode so you need an 'N' in front of the string you're inserting.
(N'this is a test ☢️') instead of ('this is a test ☢️')
This works: Fiddle
INSERT INTO table_(col) VALUES (N'this is a test ☢️');
SELECT * FROM table_;
returns
-------------------
|col |
--------------------
|this is a test ☢️|

Related

Inserting data from excel sheet into sql temp table

I have created the following temp table in SQL Server Management Studio:
CREATE TABLE ##LoginMap
(
ObjectId NVARCHAR(50),
UserPrincipleName NVARCHAR(500),
Username NVARCHAR(250),
Email NVARCHAR(500),
Name NVARCHAR(250)
)
These are the same names as the column headers in the excel sheet I am trying to get the data from.
The file name is called Test Loadsheet and the sheet name is AgilityExport_04Aug2022_164839.
I am trying to insert the data into the temp table like so:
INSERT INTO ##LoginMap
SELECT *
FROM OPENROWSET('Microsoft.ACE.OLEDB.12.0',
'Excel 12.0; Database=C:\temp\Test LoadSheet.xlsx', [AgilityExport_04Aug2022_164839]);
GO
However, I am getting the error:
The OLE DB provider "Microsoft.ACE.OLEDB.12.0" for linked server "(null)" does not contain the table "AgilityExport_04Aug2022_164839". The table either does not exist or the current user does not have permissions on that table.
Where have I gone wrong with this? And what do I need to do in order to successfully get the data from each column into my table?
You have file name as Test Loadsheet in one spot, but then in your query you have it as Test LoadSheet.xlsx. Try and see if this is holding it up.
Found a link on importing data from excel to SQL if you are interested:
https://learn.microsoft.com/en-us/sql/relational-databases/import-export/import-data-from-excel-to-sql?view=sql-server-ver16#openrowset
I went about this a different way. In the excel sheet I am using a made a formula like so:
="INSERT INTO ##LoginMap(ObjectId, DisplayName, AzureEmail, AzureUsername) VALUES('"&A2&"', '"&B2&"', '"&C2&"', '"&D2&"');"
I then had this repeated for each row. This then gave me an insert statement for each that I could simply copy and paste into SSMS and allow to run the query

Import text from excel file into SQL query

I'm running Microsoft SQL Server 17.7 and I'm trying to write script that can read text from an excel file and insert the data (Matterlist #s) as a string into the Declare statement below. I want to avoid having to copy paste each xxxxx-xxxxx number manually into my Declare statement. All I can find are ways to import data from Excel into a SQL table but nothing on how to read text and insert that data into my script. Any suggestions or steps that I could follow to get through this would greatly be appreciated it.
Declare statement:
DECLARE #MatterList NVARCHAR(100) = '(XXXXX-XXXXX, XXXXX-XXXXX, XXXXX-XXXXX, XXXXX-XXXXX)'
Excel file structure:
ID Client Matter_Name Matter_ID
------------------------------------------
1 Client1 Matter_Name1 xxxxx-xxxxx
2 Client1 Matter_Name1 xxxxx-xxxxx

Inserting data into SQL table from SAS dataset isn't working as expected

I am attempting to insert a SAS dataset into an existing table on a SQL Server. This is via a simple proc sql statement.
proc sql;
insert into Repo.Test_Table
select * from Work.MetaTable;
quit;
One of the fields, [Method], is not inserting into the SQL table as expected. The [Method] field in the SAS table contains several brackets and other punctuation so I think this is causing a problem. For example, the Work.MetaTable looks like this:
Field_ID
Method
1
([Field_1]<=[Field_8])
2
([Field_4]=[Field_5])
When I run the proc sql to insert this into SQL, it only inserts the first open bracket "(" and this is the case for every row. For example, those two rows look like this in the SQL table:
Field_ID
Method
1
(
2
(
The [Method] field in SQL is nvarchar(max).
Does anyone know what might be causing the issue here and how I can get around it?

Storing the value of ENCRYPTBYPASSPHRASE()

I am using the below query in SQL Server Management Studio 2017.
SELECT ENCRYPTBYPASSPHRASE('xxyy','test#123')
When I run the above statement, it returned an encrypted password like 0x01000000EA686E7D1AED8C501B193A2F655368FC3EABA009082C90F58987DD0487833C62
I wanted to store this in a table which contains a NVARCHAR(MAX) field using a stored procedure, but insertion happens with a blank value instead of the encrypted password.
I used a print statement to acquire the value emitted by this function with in the SP. It returned some unreadable characters as below.
됏㬷病譽快
How to use ENCRYPTBYPASSPHRASE() properly in order to insert the return value to a table field.
You can try the following Sql statement
CREATE TABLE #TempPassword (encPassword varbinary(250))
declare #encPwd varbinary(250)
set #encPwd = (SELECT ENCRYPTBYPASSPHRASE('xxyy','test#123'))
insert into #TempPassword values (#encPwd)
select * from #TempPassword
Now the output after insert it is as shown below
encPassword
0x0100000023C4E9BFBC0F4319735ED2F0C76C2D857C114D96867711F1EE290AB2E7511961

How to improve data insert/update performance?

I need to improve the performance of data loading. The current algorythm makes a full select from a table:
select Field1, Field2,...,FieldN from Table1 order by FieldM
The new data is read from a text file (say, textfile line per datatable row).
The table has a primary key, containing two fields. For each line of a textfile it locates the necessary row by these two fields (i.e. the primary key).
query.Locate('Field1;Field2',VarArrayOf([Value1,Value2]),[]);
If Locate returns True, it edits the row, otherwise it appends a new one.
So, as far as the table consists of about 200000 rows, each Locate operation takes certain amount of time...so it manages to update about 5-6 rows per second.
What things should I consider to improve it?
Probably replace locating through this great select with separate queries?
DON'T use Locate(). If you use locate() then Delphi searches row on the client side just scanning row set from your query it takes a LOT of time.
If you have access to MSSQL to create stored procedures then create following procedure and just run it for each line from your TEXT file without any conditions (Use TAdoStoredProc.ExecProc in Delphi). So in this case your don't need first select and Locate procedure. It updates record if Filed1 and Field2 are found and insert if don't.
CREATE PROCEDURE dbo.update_table1
#Field1 int, --key1
#Field2 int, --key2
#Field3 int, -- data fileds
#Field4 int
AS
SET NOCOUNT ON
update table1 set Field3=#Field3,Field4=#Field4
where Field1=#Field1 and Field2=#Field2;
IF(##Rowcount=0)
BEGIN
insert into table1(Field1,Field2,Field3,Field4)
values (#Field1,#Field2,#Field3,#Field4);
END
GO
Here is Delphi code to invoke this stored procedure with ADO:
......
var
ADOStoredP: TADOStoredProc;
......
begin
........
ADOStoredP:=TADOStoredProc.Create(nil);
try
ADOStoredP.Connection:=DataMod.SQL_ADOConnection; //Your ADO Connection instance here
ADOStoredP.ProcedureName:='Update_table1';
ADOStoredP.Parameters.CreateParameter('#Field1', ftInteger, pdInput, 0, 0);
ADOStoredP.Parameters.CreateParameter('#Field2', ftInteger, pdInput, 0, 0);
ADOStoredP.Parameters.CreateParameter('#Field3', ftInteger, pdInput, 0, 0);
ADOStoredP.Parameters.CreateParameter('#Field4', ftInteger, pdInput, 0, 0);
While () -- Your text file loop here
begin
ADOStoredP.Parameters.ParamByName('#Field1').Value:=Field1 value from text file here;
ADOStoredP.Parameters.ParamByName('#Field2').Value:=Field2 value from text file here;
ADOStoredP.Parameters.ParamByName('#Field3').Value:=Field3 value from text file here;
ADOStoredP.Parameters.ParamByName('#Field4').Value:=Field4 value from text file here;
ADOStoredP.ExecProc;
end
finally
if Assigned(ADOStoredP) then
begin
ADOStoredP.Free;
end;
end;
........
end;
If it is possible, then you should send the text file to the server running SQL Server. Then use OPENROWSET(BULK) to open the text file (see "E. Using the OPENROWSET BULK provider with a format file to retrieve rows from a text file").
If you cannot send the text file to the server, then create a temporary or persistent DB table and use INSERT to insert all text file rows into the table.
If you are using SQL Server 2008, then you should use MERGE operator. If more old SQL Server version, then you can use two SQL commands: UPDATE and INSERT. And as a data source use (1) OPENROWSET or (2) DB table.

Resources