SQL syntax conversion error with mysterious data - sql

I'm getting this message in an SQL Server 2000 stored procedure:
Syntax error converting the varchar value '...............' to a
column of data type int.
Basically I'm trying to insert data and use it in an existing stored procedure, so its possible that I"m inserting bad data, but I'm not inserting '...............' anywhere, and I can't find that anywhere related.
Trouble is, I can't find '...............' anywhere in the table I'm querying. How can I find the source of this error?
EDIT
Part of the Stored Procedure throwing error:
Select #iFromLocationID = IsNull(RecID,0)
From InventoryLocations
Where LocItemNumber = #vItemNumber
And IsNull(SkidNumber,'') <> ''
And Cast(SkidNumber as int) = #iSkidFlagID
And Warehouse = #cFromWarehouse
And Aisle = #cFromAisle
And Slot = #cFromSlot
And locLevel = #cFromLevel
And Bin = #cFromBin
Table Schema for InventoryLocations:
CREATE TABLE [dbo].[InventoryLocations](
[recid] [int] IDENTITY(1,1) NOT NULL,
[LocItemNumber] [char](16) NOT NULL,
[WareHouse] [char](2) NOT NULL,
[Aisle] [char](3) NOT NULL,
[Slot] [char](3) NOT NULL,
[locLevel] [char](2) NOT NULL,
[Bin] [char](2) NOT NULL,
[Extra] [char](2) NOT NULL,
[LocNumber] [char](2) NOT NULL,
[RollNumber] [char](20) NOT NULL,
[QuickRoll] [int] NOT NULL,
[SkidNumber] [char](15) NOT NULL,
[RollsInStock] [int] NOT NULL,
[LocQtyOnHand] [float] NOT NULL,
[LocQtyOnOrder] [float] NOT NULL,
[LocQtyCommited] [float] NOT NULL,
[TotalReceived] [float] NOT NULL,
[TotalIssued] [float] NOT NULL,
[TotalDollars] [float] NOT NULL,
[Capacity] [float] NOT NULL,
[AvailableSpace] [float] NOT NULL,
[bkey0] [char](30) NULL,
[bkey1] [char](30) NULL,
[bkey2] [char](30) NULL,
[bkey3] [char](14) NULL,
[LastPhysicalCountDate] [datetime] NULL,
[LastCycleCountDate] [datetime] NULL,
[EnteredBy] [varchar](50) NULL,
[EnteredDateTime] [datetime] NULL,
CONSTRAINT [IX_InventoryLocations_1] UNIQUE NONCLUSTERED
(
[LocItemNumber] ASC,
[WareHouse] ASC,
[Aisle] ASC,
[Slot] ASC,
[locLevel] ASC,
[Bin] ASC,
[Extra] ASC,
[RollNumber] ASC,
[SkidNumber] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

If you're building dynamic sql, try printing the query before executing it. If not, try running the queries in the stored procedure individually in an editor. If you have an INSERT..SELECT, try only running the SELECT part of the query.

This has to be the issue:
And IsNull(SkidNumber,'') <> ''
And Cast(SkidNumber as int) = #iSkidFlagID
Try running the following and see if you get an error:
SELECT CAST(SkidNumber as INT)
FROM InventoryLocations
Since #iSkidFlagID is an INT and SkidNumber is CHAR(15) it has to be where the error is occurring.

It looks like a null value was causing SQL to return the '..................'. I've changed the values I'm inserting and now I don't get that error anymore.

Related

SQL AZURE : An error occurred while executing GlobalQuery operation: Large object column support is limited to only nvarchar(max) data type

Sql azure query: after creating external table i run select query to get data from external table but this error occures!
i removed all columns with datatype = nvarchar(max) but also the problem have not been solved yet!
Code to create external table:
CREATE External TABLE [dbo].[tbl_threads_controlPanel_v](
[thread_id] [varchar](6) NOT NULL,
[thread_desc_criteria] [varchar](300) NOT NULL,
[thread_desc_formula] [varchar](300) NOT NULL,
[thread_type] [char](1) NOT NULL,
[detectType] [char](1) NOT NULL,
[detailed_qry] [nvarchar](300) NULL,
[bottomup_qry] [nvarchar](300) NULL,
[period_desc] [char](1) NULL,
[period_value] [int] NULL,
[period_value_range] [varchar](50) NULL,
--[cond_attribute] [nvarchar](max) NULL,
[cond_min_max_limit] [varchar](30) NULL,
[cond_desc] [varchar](60) NULL,
[active] [char](1) NULL,
[mature] [char](1) NULL,
[pkg_run] [char](1) NULL,
[thread_index] [int] NULL,
[thread_weight] [numeric](12, 11) NULL,
[thread_noti_type] [char](1) NULL,
[notif_id] [varchar](9) NULL,
[amt_type] [nchar](5) NULL--,
--[report_Columns] [nvarchar](max) NULL,
--[OS_Columns] [nvarchar](max) NULL
)
with(DATA_SOURCE = MyElasticDBQueryDataSrc3)
And this is the select query:
select * from dbo.[tbl_threads_controlPanel_v]
Please help ..
Thanks in advance.
UPDATE 2:
This is original tbl_threads definition
USE [DB_IFDPS_ControlPanel]
GO
/****** Object: Table [dbo].[tbl_threads] Script Date: 10/26/2016 8:51:09 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[tbl_threads](
[thread_id] [varchar](6) NOT NULL,
[thread_desc_criteria] [varchar](300) NOT NULL,
[thread_desc_formula] [varchar](300) NOT NULL,
[thread_type] [char](1) NOT NULL,
[detectType] [char](1) NOT NULL,
[detailed_qry] [text] NULL,
[bottomup_qry] [text] NULL,
[period_desc] [char](1) NULL,
[period_value] [int] NULL,
[period_value_range] [varchar](50) NULL,
[cond_attribute] [nvarchar](max) NULL,
[cond_min_max_limit] [varchar](30) NULL,
[cond_desc] [varchar](60) NULL,
[active] [char](1) NULL,
[mature] [char](1) NULL,
[pkg_run] [char](1) NULL,
[thread_index] [int] NULL,
[thread_weight] [numeric](12, 11) NULL,
[thread_noti_type] [char](1) NULL,
[notif_id] [varchar](9) NULL,
[amt_type] [nchar](5) NULL,
[report_Columns] [nvarchar](max) NULL,
[OS_Columns] [nvarchar](max) NULL,
CONSTRAINT [PK_tbl_threads] PRIMARY KEY CLUSTERED
(
[thread_id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
It looks like this is not the whole story.
The problem seems to be not with the varchar(max) columns but with a column that is defined somewhere else as a LOB but in the external table is defined otherwise than varchar(max)
Thanks for adding the relevant DDL.
The issue is most likely with the text columns
CREATE External TABLE [dbo].[tbl_threads_controlPanel_v](
...
[detailed_qry] [nvarchar](300) NULL,
[bottomup_qry] [nvarchar](300) NULL,
...
CREATE TABLE [dbo].[tbl_threads]
...
[detailed_qry] [text] NULL,
[bottomup_qry] [text] NULL,
...
P.s.
https://msdn.microsoft.com/en-us/library/ms187993.aspx
IMPORTANT! ntext, text, and image data types will be removed in a future version of SQL Server. Avoid using these data types in new development work, and plan to modify applications that currently use them. Use nvarchar(max), varchar(max), and varbinary(max) instead.

In Trigger getting error as error converting data type varchar to numeric

I have a trigger which was working earlier perfectly. But later on, as required I added some more lines into that which is below
declare #imkey numeric(10,0);declare #xmkey numeric(10,0); declare #xsrno numeric(10,0)
select #xmkey=max([Mkey])+1, #xsrno=max([Entry_Sr_No])+1
from erp190516.[dbo].[Inward_Doc_Tracking_Trl] where Ref_Mkey=#imkey;
select #imkey=i.Inward_ref_key from inserted i
insert into erp190516.[dbo].[Inward_Doc_Tracking_Trl]
SELECT #xmkey,#xsrno,[N_UserMkey],'1' [N_Department], 'F' [CStatus_Flag]
,'Requester' [Remarks],'1' [CUser_ID],getdate() [U_Datetime],
,'N' [NStatus_Flag], 'N' [Delete_Flag]
,'1'[CDept_Id],[Ref_Mkey],[No_Of_Days],[Approved_Amount],[Chq_No],[Chq_dated]
,[Chq_Bank],[Chq_Amount],[Vendor_MKey],[Vendor_Comp_Mkey]
,[Project_Mkey],[Program_mkey],[Payment_MKey],[Due_Date],[Updated_Remarks]
,[Updated_Bill_no],[Updated_Bill_Date],[Updated_Bill_Amt]
,[Party_Name],[Acc_mkey],[TotalDeductions],[Broker_Mkey],[Customer_Mkey]
,[Payable_Amt],[Balance_Amt],[Receipt_No],[Po_No],[Bill_No]
,[Disp_through],[Disp_Through_Name],[Site_Id]
FROM erp190516.[dbo].[Inward_Doc_Tracking_Trl] where Ref_Mkey=#imkey
It eexcuted perfectly, but while inserting the data into the table I got error as
Error converting data type varchar to numeric
Table Definition
CREATE TABLE [dbo].[Inward_Doc_Tracking_Trl](
[Mkey] [numeric](18, 0) NOT NULL,
[Entry_Sr_No] [numeric](4, 0) NOT NULL,
[N_UserMkey] [numeric](10, 0) NULL,
[N_Department] [numeric](10, 0) NULL,
[CStatus_Flag] [numeric](8, 0) NOT NULL,
[Remarks] [varchar](500) NULL,
[CUser_ID] [numeric](10, 0) NOT NULL,
[U_Datetime] [datetime] NOT NULL,
[NStatus_Flag] [numeric](10, 0) NOT NULL,
[Delete_Flag] [char](1) NULL,
[CDept_Id] [numeric](10, 0) NOT NULL,
[Ref_Mkey] [numeric](18, 0) NULL,
[No_Of_Days] [int] NULL,
[Approved_Amount] [float] NULL,
[Chq_No] [varchar](50) NULL,
[Chq_dated] [datetime] NULL,
[Chq_Bank] [varchar](40) NULL,
[Chq_Amount] [float] NULL,
[Vendor_MKey] [int] NULL,
[Vendor_Comp_Mkey] [int] NULL,
[Project_Mkey] [numeric](10, 0) NULL,
[Program_mkey] [numeric](10, 0) NULL,
[Payment_MKey] [int] NULL,
[Due_Date] [datetime] NULL,
[Updated_Remarks] [varchar](500) NULL,
[Updated_Bill_no] [varchar](27) NULL,
[Updated_Bill_Date] [datetime] NULL,
[Updated_Bill_Amt] [float] NULL,
[Party_Name] [varchar](80) NULL,
[Acc_mkey] [numeric](10, 0) NULL,
[TotalDeductions] [float] NULL,
[Broker_Mkey] [numeric](10, 0) NULL,
[Customer_Mkey] [numeric](10, 0) NULL,
[Payable_Amt] [float] NULL,
[Balance_Amt] [float] NULL,
[Receipt_No] [varchar](50) NULL,
[Po_No] [varchar](50) NULL,
[Bill_No] [varchar](50) NULL,
[Oracle_doc_no] [varchar](100) NULL,
CONSTRAINT [PK_Inward_Doc_Tracking_Trl_1] PRIMARY KEY CLUSTERED
(
[Mkey] ASC,
[Entry_Sr_No] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
ALTER TABLE [dbo].[Inward_Doc_Tracking_Trl] ADD CONSTRAINT [DF_Inward_Doc_Tracking_Trl_U_Datetime] DEFAULT (getdate()) FOR [U_Datetime]
GO
ALTER TABLE [dbo].[Inward_Doc_Tracking_Trl] ADD CONSTRAINT [DF__Inward_Do__Delet__47FED732] DEFAULT ('N') FOR [Delete_Flag]
GO
You are inserting character values in [NStatus_Flag] and [CStatus_Flag] , Just change their data type in table or insert data with numeric value into them.
Well - one obvious error is that you have [CStatus_Flag] [numeric](8, 0) but select 'F' [CStatus_Flag].
Also just for clarity sake, but you shouldn't write '1' for selection of numeric fields either, when 1 can do it without conversions.

Conversion failed when converting date and/or time from character string. What could be wrong?

INSERT INTO dbo.SaleNew(ENQ_AID,DOCKET_NO,SALE_TYPE,VEHICLE_MODEL,SALE_DATE,BOOKING_DATE,DELIVERY_DATE,
DEALER_NAME,ENQ_GEN_BY,EXEC_NAME,USER_CR,DATE_CR) SELECT '6','2','0','TEST','2016-05-01','2016-05-10','2016-05-15',
'ABC','S I','V B','1',SWITCHOFFSET(SYSDATETIMEOFFSET(), '+05:30')
What could be wrong with this query?
Also tried:
INSERT INTO dbo.SaleNew(ENQ_AID,DOCKET_NO,SALE_TYPE,VEHICLE_MODEL,SALE_DATE,BOOKING_DATE,DELIVERY_DATE,
DEALER_NAME,ENQ_GEN_BY,EXEC_NAME,USER_CR,DATE_CR) SELECT '6','2','0','TEST',CONVERT(DATE,'01/05/2016',103),CONVERT(DATE,'10/05/2016',103),
CONVERT(DATE,'15/05/2016',103),'ABC','S I','V B','1',SWITCHOFFSET(SYSDATETIMEOFFSET(), '+05:30')
This is the create table query:
CREATE TABLE [dbo].[SaleNew](
[SALE_ID] [int] IDENTITY(1,1) NOT NULL,
[ENQ_AID] [bigint] NOT NULL,
[DOCKET_NO] [varchar](50) NOT NULL,
[VEHICLE_MODEL] [varchar](100) NOT NULL,
[SALE_DATE] [date] NULL,
[BOOKING_DATE] [date] NULL,
[DELIVERY_DATE] [date] NULL,
[DEALER_NAME] [date] NULL,
[ENQ_GEN_BY] [varchar](100) NULL,
[EXEC_NAME] [varchar](100) NULL,
[USER_CR] [int] NULL,
[DATE_CR] [date] NULL,
[USER_UP] [int] NULL,
[DATE_UP] [date] NULL,
[SALE_TYPE] [int] NOT NULL,CONSTRAINT [PK_SaleNew] PRIMARY KEY CLUSTERED (
[SALE_ID] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON ) ON [PRIMARY]
What could be the reason?
DEALER_NAME type is defined as DATE but you want to INSERT a VARCHAR type value ABC in it.
Execute following query in order to edit the mentioned column type, then Execute your INSERT query.
ALTER TABLE [dbo].[SaleNew]
ALTER COLUMN [DEALER_NAME] VARCHAR(50) NULL

How to copy column Data of one Table to another

I am using the code below:
Insert into JV(PurchaseID)
select PurchaseID from PurchaseReturnDetail
But when I execute the query it gives an error:
Cannot insert the value NULL into column 'column1', table 'database.table1'; column does not allow nulls.
Then my INSERT fails. What could be the issue?
The error says the table has a non-null constraint for the column. To change the column to allow nulls, run this query (for Oracle DB):
alter table TABLE_NAME modify (COLUMN_NAME null);
If first columns is identity column, make sure that auto increment is enabled for that column .
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[JV](
[JVID] [numeric](18, 0) IDENTITY(1,1) NOT NULL,
[JVInvoiceNo] [nvarchar](50) NULL,
[JVDate] [datetime] NULL,
[StoreID] [numeric](18, 0) NULL,
[BusinessID] [numeric](18, 0) NULL,
[UserID] [numeric](18, 0) NULL,
[Currency] [nvarchar](50) NULL,
[Rate] [decimal](18, 10) NULL,
[DueDate] [datetime] NULL,
[Reference] [nvarchar](50) NULL,
[RefID] [numeric](18, 0) NULL,
[Narration] [nvarchar](max) NULL,
[InvoiceRef] [nvarchar](50) NULL,
[Status] [nvarchar](50) NULL,
[Type] [nvarchar](50) NULL,
[PurchaseID] [numeric](18, 0) NULL,
CONSTRAINT [PK_JV] PRIMARY KEY CLUSTERED
(
[JVID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO

TSQL Computed column limitations

CREATE TABLE [dbo].[MembershipModule](
[Id] [uniqueidentifier] ROWGUIDCOL NOT NULL,
[ParentId] [uniqueidentifier] NULL,
[TargetId] [int] NULL,
[WebContentId] [uniqueidentifier] NULL,
[Name] [varchar](35) NOT NULL,
[NameUpper] AS (isnull(upper([Name]),'')) PERSISTED NOT NULL,
[UriPrefix] [varchar](max) NULL,
[UriText] [varchar](max) NULL,
[UriComputed] AS ??? PERSISTED,
[Description] [varchar](100) NULL,
[Created] [date] NOT NULL,
[Modified] [datetime2](7) NOT NULL,
[MenuItem] [bit] NOT NULL,
[Enabled] [bit] NOT NULL,
[Position] [smallint] NULL,
CONSTRAINT [PK_MembershipModule] PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
So far the UriComputed field is computed like this:
lower(replace(isnull([UriPrefix],'/')+coalesce([UriText],[Name]),' ','-'))
This produces output like the following
Now, I'd want to terminate all UriComputed values with '/'. This would be easily accomplished by adding + '/' to the computed field, except for the fact that "textless" uris, would be terminated like //, which I don't want happening.
since the sql I can put into a computed field is quite limited (and I don't really know the extents of these limitations) I thought I'd ask here how to add this.
basically I want the output in the image to be
/a/login/
/a/announcements/
/a/
/
my closest attempt at doing this has been:
isnull(convert(varchar(MAX),nullif(len(coalesce([UriText],[Name])),0)),'/')
Which does kind of a mess, and adds a number if it should terminate in '/', and adds '/' when it should, what I'd need is the opposite ( that is, '/' when the length is 0, '' otherwise)
If there's an inline if or something like that I could use that would basically be it, but I don't know about that.
Thank you!
This worked for me:
[UriComputed] AS (CASE
WHEN RIGHT(lower(replace(isnull([UriPrefix],'/')+coalesce([UriText],[Name]),' ','-')), 1) = '/' THEN
lower(replace(isnull([UriPrefix],'/')+coalesce([UriText],[Name]),' ','-'))
ELSE
lower(replace(isnull([UriPrefix],'/')+coalesce([UriText],[Name]),' ','-')) +'/'
END) PERSISTED,
Full CREATE TABLE statement:
CREATE TABLE [dbo].[MembershipModule](
[Id] [uniqueidentifier] ROWGUIDCOL NOT NULL,
[ParentId] [uniqueidentifier] NULL,
[TargetId] [int] NULL,
[WebContentId] [uniqueidentifier] NULL,
[Name] [varchar](35) NOT NULL,
[NameUpper] AS (isnull(upper([Name]),'')) PERSISTED NOT NULL,
[UriPrefix] [varchar](max) NULL,
[UriText] [varchar](max) NULL,
[UriComputed] AS (CASE
WHEN RIGHT(lower(replace(isnull([UriPrefix],'/')+coalesce([UriText],[Name]),' ','-')), 1) = '/' THEN
lower(replace(isnull([UriPrefix],'/')+coalesce([UriText],[Name]),' ','-'))
ELSE
lower(replace(isnull([UriPrefix],'/')+coalesce([UriText],[Name]),' ','-')) +'/'
END) PERSISTED,
[Description] [varchar](100) NULL,
[Created] [date] NOT NULL,
[Modified] [datetime2](7) NOT NULL,
[MenuItem] [bit] NOT NULL,
[Enabled] [bit] NOT NULL,
[Position] [smallint] NULL)