Derive column values from other columns using a stored procedure in Azure Data Factory - sql

I'm trying to concatenate values from two columns and put it in the third column using a stored procedure. But I'm getting error.
This is my stored procedure:
create proc deriveColumn_SP
#col_1 varchar(20),
#col_2 varchar(20)
as
begin
insert into tableName(col_5)
values concat(#col_1, #col_2)
end
I get an error message that reads
Incorrect syntax near 'concat'
I want the derived value to be in col_1_value, col_2_value format. Data type of all the columns are varchar (varchar(50) for col_5).
Please help

insert into tableName(col_5)
values (concat(#col_1, #col_2))
or just use a select
insert into tableName(col_5)
select concat(#col_1, #col_2)

Related

SELECT statement with IN and input variable in SQL server [duplicate]

This question already has answers here:
T-SQL stored procedure that accepts multiple Id values
(5 answers)
Closed 2 years ago.
Having a table (Table1) with Columns -> Column1 int, Column2 int, Column3 varchar(128), Column4 char(11)
Need to create a stored procedure to get the details from Table1 based on the input parameter. Input parameter holds values with comma-separated. It has to be mapped with Column1
Create Procedure ProcedureName(#InputParams VARCHAR(MAX))
AS
BEGIN
SELECT Column2
,Column3
,Column4
FROM Table1
WHERE Column1 IN (#InputParams)
ORDER BY Column2
END
The below statement is throwing conversion error
"Conversion failed when converting the varchar value '123, 456, 789' to data type int."
EXEC ProcedureName #InputParams = '123, 456, 789'
Converting the SELECT statement to the dynamic query will work fine but will we able to fix the conversion issue in the static query. Please help me.
Well, you have a couple of options in this case :
Table-valued input parameter : For this, you'll have to create a UDT (user-defined table type) and use that as the parameter to your stored procedure.
Convert the comma-separated input string parameter into integers and then use them in the IN clause.
Not sure about your use-case, but generally, I would suggest going forward with the first option.

Why this simple stored procedure isn't working

Here is the deal, I am receiving an array from C# and I want to insert it into the following table with only 2 columns which are #idUser int and #idRegion int.
The stored procedure needs to receive the array and insert it into the table but somehow it isn't working, it tells me that it cannot convert #idRegion to an int. I tried to use CAST and CONVERT to convert it into int but it isn't working.
The Select From works ok, but not the insert.
Here is the stored procedure (#idUser needs to be the same for all inserted rows):
#idUser int,
#idRegion nvarchar(MAX)
AS
BEGIN
INSERT INTO [UsersRegion] (idUser,IdRegion)
VALUES (#idUser, #idRegion)
SELECT #idUser,cast(value as int) FROM STRING_SPLIT(#idRegion,',')
END
I get this error when running it:
Conversion failed when converting the nvarchar value '1,2,3,4' to data type int.
If you are sending multiple values in #idRegion then when you split them, you may have more than 1 things you need to insert. Therefore, do it like this:
INSERT INTO [UsersRegion] (idUser,IdRegion)
SELECT #idUser, value FROM STRING_SPLIT(#idRegion, ',')
If the target table's IdRegion column is of type int, you need to cast like this:
SELECT #idUser, cast(value as int) FROM STRING_SPLIT(#idRegion, ',')
Above code will insert the same #idUser for every record but a different value for IdRegion depending the splitted items. More on Insert into select from
Your INSERT statement seems to be working with IdRegion while everything else is lowercase id.
However, assuming this is how the actual table column is named and is not a typo...
Your problem is most likely the line that reads:
#idRegion nvarchar(MAX)
Which is declaring the #idRegion variable as a string, while you have stated in the question that it's meant to be an int.
This would explain the casting error.
If you cannot pass it into the procedure as an int from the C# code. Your only other option would be to try to parse it into an int as you have said.

SQL String or binary data would be truncated. while inserting into table type parameter

I have a scenario where there is a table and i nned to pass table values param inside one of the stored procedure to peform certain actions . Basic table structure is as follows.
CREATE TABLE [dbo].[CitytTax] (
[CountryCode] [int] NOT NULL,
[TaxType] [varchar](255) NULL
)
As you see, Taxtype column is varchar type and takes upto 255 chars. However I will create on table type as below in part of application code and pass this table type as param to one of sp.
DECLARE #TaxDetails as [CitytTax]
Now i will insert some dummy values into it and pass this table type to one of the SP.
INSERT INTO #TaxDetails ([CountryCode],TaxType )
VALUES (6047,'Codfggtuioanoio charge to fund the liquidation of insurancevalues')
but getting error as below :
String or binary data would be truncated
The question here is table value type is having a column which is similar to actual database table. SO when i insert above script, it fails. However if i insert any value to [taxtype] which is less than 50 characters, it will insert successfully. but fails for more than 50 chars. IM wondering why it fails,it is supposed to take upto 255 characters right??
If your data length is indeed shorter than the field length. Then you're having having another table filled by a trigger on the main table, where the column size also had to be changed.
Also, replace all single quotes of your insert into query to double quotes and pass it into the stored procedure.
Well I figured out something which is a quick fix: just added below line before insert query:
SET ANSI_WARNINGS OFF .

Bulk inserting data gives error

Attempting to bulk insert into a table and I am getting the error:
Bulk load data conversion error (type mismatch or invalid character for the specified codepage) for row 31, column 4 (Birthday).
Below is the code I am trying to use to insert the data:
Bulk Insert Dzt.dbo.Player
From 'A:\New Folder\Seed_Files\Player.csv'
With
(
FieldTerminator=',',
RowTerminator='\n',
FirstRow=2
)
Here is the code I used when making the table:
Use Dzt
Create Table Player
(
Player_ID int,
FirstName varchar(255),
LastName varchar(255),
Birthday date,
Email varchar(255),
L_Flag varchar(255)
);
This is my first attempt at making a table and inserting data so I am thinking it is likely a datatype error for the Birthday field but I have been unable to find anything online that I am able to grasp my head on at this time. I have also tried use the datatype datetime instead of date but I received the same error.
I am using SSMS 2012 to create and insert the data onto a 2012 SQL Server.
Let me know if there is anything else I can provide that might help.
As you suspect it could be a date format error, I would suggest importing the csv into a table with Birthday column set to varchar type. Then use this query to filter the erroneous records.
select birthday from temptable where isdate(birthday) = 0
You could then correct those records and then insert them into your old table.

Unable to delete right to left language columns using stored procedure

I'm using stored procedure to delete a row from MSSQL database based on a column that uses nvarchar(100) and Persian language.
when i want to insert into this column, i use the word N before the record to be able to perform the insert operation :
insert into materialPrice values( N'persian word',1000,100,0,0,0,0)
the problem is when i want to delete the same record, stored procedure does not work :
create proc spRemoveMaterial
#materialName nvarchar(100)
as
begin
delete from materialPrice where materialName = #materialName
end
I've tried to use N before #materialName but it returend syntax error. how could it be done ?
The N is a marker that causes the string literal to be represented in Unicode--implying that you are inserting into a Unicode column.
You should be able to convert the variable to Unicode with cast. Something like:
cast(#materialName as nvarchar(100))
With the correct type (nchar or nvarchar) and length to match the column.
The problem was with the database collation, following code has fixed it :
ALTER database MaterialDB COLLATE Persian_100_CI_AS