Got error while inserting Dataset into a database Table - sql

This is the error
String or binary data would be truncated.
The data for table-valued parameter "#tblCustomers" doesn't conform to the table type of the parameter. SQL Server error is:
Msg 8152, state: 10
The statement has been terminated.
Here is the user-defined table type
CREATE TYPE [dbo].[recType] AS TABLE
(
[refid] [int] NULL,
[fromid] [varchar](13) NULL,
[toid] [varchar](13) NULL,
[message] [int] NULL,
[status] [varchar](13) NULL,
)
Here is the stored procedure
CREATE PROCEDURE [dbo].[SP_INSERT_RECVSMS]
#tblCustomers recType READONLY
AS
BEGIN
SET NOCOUNT ON;
INSERT INTO RecieveSMS(Refid, fromID, Toid, message, status)
SELECT
refid, fromid, toid, message, status
FROM #tblCustomers
END

Error message is very clear. Target/source table column is to small to hold string value.
Change the size for string columns to appropriate values:
[fromid] [varchar](13) NULL
--
[fromid] [varchar](xxx) NULL,

Related

How to Pass API Response to Azure SQL Table via Stored Procedure

I have a Azure Data Factory pipeline that uploads data to Salesforce and then gets the response back to Azure Data Factory. I am trying to get the response from the failed job and store in my failed records table in Azure SQL. I am using Stored Procedure activity to pass on the values to the table. The issue I am having is that the stored Procedure activity is getting the correct input for response but when it passes the value to SQL it just insert ".
I previously had the failed_records_details column set as Varchar(max) and then tried to change it to NText and see if SQL would accept the string value passes on from the API response. But I haven't had any luck so far. Any help would be really appreciated.
Update
OP changed his table structure to varchar(max) and it worked.
After many tests, I found the answer. I used OPENJSON to process in stored procedure.
This is my table:
CREATE TABLE [dbo].[product](
[PRODUCT_ID] [int] NULL,
[PRODUCT_TYPE] [int] NULL,
[PRODUCT_NAME] [varchar](50) NULL,
[PRODUCT_TITLE] [varchar](255) NULL,
[PRODUCT_PIC] [varchar](255) NULL,
[CREATE_TIME] [datetime] NULL,
[UPDATE_TIME] [datetime] NULL,
[PRODUCT_INTRO] [text] NULL,
[PRODUCT_FEATURE_ID] [varchar](255) NULL,
[PRODUCT_PARAM] [varchar](255) NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
This is my API response:
{
"productId": 2,
"productTypeId": 2,
"productName": "AD2",
"productTitle": "TopAir AD215-1000A",
"productPic": "/img/productPic/1593684705859user.jpg",
"createTime": "2020-06-05 09:17:31",
"updateTime": "2020-06-05 09:17:31",
"productFeatureId": "",
"productParam": "/img/productPic/param/1593685548627a32415ca9a21f6f9a1d99b2731f224b5d319c424.jpg",
"productIntro": "",
"productType": null,
...
This is my stored procedure, the api request as a string type input parameter.
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[uspProduct] #product NVARCHAR(MAX)
AS
BEGIN TRY
INSERT INTO dbo.product(PRODUCT_ID,PRODUCT_TYPE,PRODUCT_NAME,PRODUCT_TITLE,PRODUCT_PIC,CREATE_TIME,UPDATE_TIME,PRODUCT_INTRO,PRODUCT_FEATURE_ID,PRODUCT_PARAM)
SELECT * FROM OPENJSON(#product)
WITH(
PRODUCT_ID int '$.productId',
PRODUCT_TYPE int '$.productTypeId',
PRODUCT_NAME varchar(50) '$.productName',
PRODUCT_TITLE varchar(255) '$.productTitle',
PRODUCT_PIC varchar(255) '$.productPic',
CREATE_TIME datetime '$.createTime',
UPDATE_TIME datetime '$.updateTime',
PRODUCT_INTRO varchar(255) '$.productIntro',
PRODUCT_FEATURE_ID varchar(255) '$.productFeatureId',
PRODUCT_PARAM varchar(255) '$.productParam'
)
END TRY
BEGIN CATCH
PRINT ERROR_MESSAGE ( )
END CATCH
;
Add dynamic content #string(activity('Web1').output). After repeated testing, here we must convert API response from object type to string type.
This is the input of the Stored procedure1 activity.
The debugging results are as follows:
I can see the API response was inserted into the table.

Merge stored procedure

CREATE TABLE [dbo].[SupplierTableType](
[Supplier_ID] [nvarchar](10) NULL,
[Supplier_Name] [nvarchar](50) NULL,
[Address1] [nvarchar](30) NULL,
[Address2] [nvarchar](30) NULL,
[Address3] [nvarchar](30) NULL,
)
GO
--------------------Procedure----------------------
CREATE PROCEDURE [dbo].[Update_SupplierTable]
#TblSupplierTable SupplierTableType READONLY
AS
BEGIN
SET NOCOUNT ON;
MERGE INTO SupplierTable c1
USING #TblSupplierTable c2
ON c1.Supplier_ID=#TblSupplierTable.Supplier_ID
WHEN MATCHED THEN
UPDATE SET
c1.[Supplier_Name]= c2.[Supplier_Name]
, c1.[Address1]=c2.[Address1]
, c1.[Address2]=c2.[Address2]
, c1.[Address3]=c2.[Address3]
WHEN NOT MATCHED THEN
INSERT VALUES( #TblSupplierTable.[Supplier_Name],
c2.[Address1],
c2.[Address2],
c2.[Address3]
);
END
Msg 2715, Level 16, State 3, Procedure Update_SupplierTable, Line 3 [Batch Start Line 56]
Column, parameter, or variable #1: Cannot find data type SupplierTableType.
Parameter or variable '#TblSupplierTable' has an invalid data type.
Msg 1087, Level 16, State 1, Procedure Update_SupplierTable, Line 8 [Batch Start Line 56]
Must declare the table variable "#TblSupplierTable".
Instead of creating a table, you have to create a data type for SupplierTableType.
I.e. instead of
CREATE TABLE SupplierTableType...
you have to use
CREATE TYPE SupplierTableType AS TABLE ...
See the docs for further info.

Msg 207, Level 16, State 1 "Invalid column name 'Name'"

I'm trying to drop and recreate a table with a new schema in SQL Server, and then insert some test data into it to validate that it's working correctly. I have the following DDL:
CREATE TABLE [dbo].[Product](
[ID] int IDENTITY(1,1) PRIMARY KEY,
[Name] VARCHAR(100) NOT NULL,
[Description] VARCHAR(200) NULL,
[Modified] DATETIME NOT NULL DEFAULT(GETUTCDATE()),
[ModifiedBy] VARCHAR(32) NOT NULL
);
and the following insert statement:
INSERT INTO [dbo].[Product]([Name], [Description], [Modified], [ModifiedBy])
VALUES('Test', 'Description', GETUTCDATE(), 'Me');
Whenever I attempt to insert the aforementioned row, I get an error:
Msg 207, Level 16, State 1, Line 38
Invalid column name 'Name'.
Msg 207, Level 16, State 1, Line 38
Invalid column name 'Description'.
I know the table is there since I can SELECT * FROM Product and get an empty result set with the correct columns and no errors... I just can't insert into it for some reason. Any help would be greatly appreciated!
As per Martin's answer, I did some more research and he is correct. The schema manipulation needs to be done in separate batches (I simply added a GO after dropping my old table and after creating my new one with a different schema). A more detailed answer can be found here: "Invalid column name" error when calling insert after table created
Maybe try refreshing the cache. Easy way is to press Ctrl + shift + R
Or
Edit > IntelliSense > Refresh Local Cache
If doesnt work, then use qualified names [dbo].table or username.tablename etc
More here: http://msdn.microsoft.com/en-us/library/ms174205.aspx
For me, your query looks good, and i test it successfully.
Try adding schema name and put all column name inside brackets:
CREATE TABLE [dbo].[Product](
[ID] int IDENTITY(1,1) PRIMARY KEY,
[Name] VARCHAR(100) NOT NULL,
[Description] VARCHAR(200) NULL,
[Modified] DATETIME NOT NULL DEFAULT(GETUTCDATE()),
[ModifiedBy] VARCHAR(32) NOT NULL
);
INSERT INTO [dbo].[Product]([Name], [Description], [Modified], [ModifiedBy])
VALUES('Test', 'Description', GETUTCDATE(), 'Me');
Don't do anything just try to insert (' ')single quotes instead of (") quotes.. or if you insert data with ('')single quotes try for (") double quotes it's really helping you.

SQL Insert - String or binary data would be truncated for INT values

I'm getting the "String or binary data would be truncated" error when trying to insert integers to one of my tables.
I've read several post about the length of the column vs the length of the value one is inserting, but it doesn't seem to be my case once the columns are all int or smallint type and the values are all maximum two digits.
The table structure is the following:
CREATE TABLE [tblvUserLocation] (
[User_Location_ID] [int] IDENTITY (1, 1) NOT NULL ,
[Location_ID] [int] NULL ,
[Line_Type_ID] [int] NULL ,
[User_ID] [int] NULL ,
[Active] [smallint] NULL CONSTRAINT [DF_tblvUserLocation_Active] DEFAULT (1),
[Last_Updated] [smalldatetime] NULL CONSTRAINT [DF_tblvUserLocation_Last_Updated] DEFAULT (getdate()),
[Last_Updated_By] [varchar] (10) COLLATE SQL_Latin1_General_CP1_CI_AS NULL CONSTRAINT [DF_tblvUserLocation_Last_Updated_By] DEFAULT (suser_sname())
) ON [PRIMARY]
GO
The insert I'm trying to run is the following:
insert into tblvUserLocation (Location_ID, Line_Type_ID, [User_ID], Active)
values (20, 2, 41, 1)
And the error I'm getting is the following:
Server: Msg 8152, Level 16, State 2, Line 1 String or binary data
would be truncated. The statement has been terminated.
If that makes any difference, I'm using SQL Server 2000.
Please let me know what your thoughts are.
Thanks!
Looks like the problem comes from your [DF_tblvUserLocation_Last_Updated_By] constraint.
It's pulling the current username which is more than likely longer than the length of your [Last_Updated_By] column VARCHAR(10).
Update your DDL to:
[Last_Updated_By] [varchar] (128)

SQL Server Msg 241, Level 16, State 1, Conversion failed when converting date and/or time from character string

I am getting this error when executing a stored procedure that I have created
Msg 241, Level 16, State 1, Procedure UDP_INITIAL_CUSTOMER_DIM_POPULATION, Line 28
Conversion failed when converting date and/or time from character string.
The code for the procedure is:
CREATE PROCEDURE UDP_INITIAL_CUSTOMER_DIM_POPULATION
AS
BEGIN
SET NOCOUNT ON
EXEC UDP_REMOVE_NULLS_FROM_CUSTOMER_STG
INSERT INTO dbo.customer_dim(customer_no, first_name, middle_name, last_name,
street_number, street_name, po_address, zip_code, city,
region, country)
SELECT DISTINCT
CAST(customer_no AS VARCHAR),
first_name,
middle_name,
last_name,
street_number,
street_name,
po_address,
zip_code,
city,
region,
country
FROM
dbo.customer_stg
PRINT 'Initial customer data in place!'
SET NOCOUNT OFF
END
and the code to create the customer_dim table is:
CREATE PROCEDURE dbo.UDP_CREATE_CUSTOMER_DIM
AS
BEGIN
IF OBJECT_ID('customer_dim', 'U') IS NOT NULL
BEGIN
DROP TABLE customer_dim
END
CREATE TABLE [dbo].[customer_dim]
(
[customer_sk] INT IDENTITY (1,1) PRIMARY KEY,
[customer_no] [varchar](50) NULL,
[first_name] [varchar](50) NULL,
[middle_name] [varchar](50) NULL,
[last_name] [varchar](50) NULL,
[customer_name] [varchar](50) NULL,
[street_number] [varchar](50) NULL,
[street_name] [varchar](50) NULL,
[customer_address] [varchar](100) NULL,
[po_address] [varchar](50) NULL,
[zip_code] [varchar](50) NULL,
[city] [varchar](50) NULL,
[region] [varchar](50) NULL,
[country] [varchar](50) NULL,
[effective_date] DATE NULL DEFAULT GETDATE(),
[expiry_date] DATE NULL DEFAULT '999-12-31'
) ON [PRIMARY]
END
I have tried looking for a solution but because I am not trying to do anything on the date or time I cannot find anything that is making sense to me.
The error is caused by the default value for the expiry_date. Running
SELECT CAST('999-12-31'AS DATE)
will produce an error:
Conversion failed when converting date and/or time from character
string.
What you want is
[expiry_date] DATE NULL DEFAULT '9999-12-31'
I'm doing string to date conversion in SQL 2017 and it properly handles strings in m/d/yyyy format with single and double digit days or months and two or four digit years. For example, the following strings converted cleanly:
2/6/2012
1/20/2020
12/21/2017
6/14/21
In my case I found the problem to be a record with a value of '5/15/161'. To find the bad data, use the ISDATE() function. In my case:
SELECT [EMPLOYER NUMBER],[PLAN],[EMPLOYER NAME],[LAST UPDATED]
FROM [CONTACT NAME ADDRESS]
where [last updated] is not null
and isdate([last updated]) <> 1
Default value should be converted to the data type of column by query executor.
Date data type requires data in " YYYY-mm-dd" fromat
Luke problem is with your default vale for date data type.
While sql is trying to convert /cast yyy-mm-dd it fails to convert it to date