Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Error converting data type varchar to numeric.

Author  Topic 

ejoeyz_85
Yak Posting Veteran

54 Posts

Posted - 2008-03-24 : 13:27:46
hello everyone... i have some problem to with my trigger.. after execute the trigger.. i got this errro "Msg 8114, Level 16, State 5, Procedure trg_InsertPVReadingMeter, Line 14
Error converting data type varchar to numeric
."


below is my command sql table PVReadingMeter

quote:
USE [PVMC Database]
GO
/****** Object: Table [dbo].[PVReadingMeter] Script Date: 03/25/2008 01:18:06 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[PVReadingMeter](
[PV_reading_id] [uniqueidentifier] NOT NULL CONSTRAINT [DF_PVReadingMeter_PV_reading_id] DEFAULT (newid()),
[PV_reading_date_time] [datetime] NOT NULL,
[PV_reading_meter] [decimal](8, 2) NOT NULL,
[PV_normalized_monthly_yield] [float] NOT NULL,
[PV_normalized_monthly_energy_yield] [float] NOT NULL,
[PV_application_id] [uniqueidentifier] NOT NULL CONSTRAINT [DF_PVReadingMeter_PV_application_id] DEFAULT (newid()),
CONSTRAINT [PK_PVReadingMeter] PRIMARY KEY CLUSTERED
(
[PV_reading_id] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]

GO
USE [PVMC Database]
GO
ALTER TABLE [dbo].[PVReadingMeter] WITH CHECK ADD CONSTRAINT [FK_PVReadingMeter_Photovoltaic] FOREIGN KEY([PV_application_id])
REFERENCES [dbo].[Photovoltaic] ([PV_application_id])
ON UPDATE CASCADE
ON DELETE CASCADE


below is my trigger

quote:
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go

CREATE Trigger [trg_InsertPVReadingMeter]
on [dbo].[PVReadingMeter]
for insert
as
begin

DECLARE @PVReadingMeter VARCHAR(100)
DECLARE @PVNormalizedMonthlyYield VARCHAR(100)


SELECT @PVReadingMeter = (SELECT PV_reading_meter + '' FROM Inserted)
SELECT @PVNormalizedMonthlyYield = (SELECT PV_normalized_monthly_yield + '' FROM Inserted)

-- Print the name of the new author

PRINT 'The new photovoltaic reading meter"' + @PVReadingMeter +'" is added.'
PRINT 'The photovoltaic normalized monthly yield is "' + @PVNormalizedMonthlyYield +'"'

end;





now, this is command to insert the values into PVReadingMeter table using trigger that has been created....

quote:
insert into PVReadingMeter (PV_reading_date_time, PV_reading_meter, PV_normalized_monthly_yield,
PV_normalized_monthly_energy_yield, PV_application_id)
values (getDate(), 1.1, 4234.33, 43.44, '3D23C4FE-F6FC-475E-8CEC-23FE4E738EB9')



so, i got this error... Msg 8114, Level 16, State 5, Procedure trg_InsertPVReadingMeter, Line 14
Error converting data type varchar to numeric.


what should i do???

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-03-24 : 13:47:23
CAST the two fields to varchar using CAST function

i.eSELECT @PVReadingMeter = (SELECT CAST(PV_reading_meter AS varchar(10)) + '' FROM Inserted)
SELECT @PVNormalizedMonthlyYield = (SELECT CAST(PV_normalized_monthly_yield AS varchar(50))+ '' FROM Inserted)
Go to Top of Page

Bustaz Kool
Master Smack Fu Yak Hacker

1834 Posts

Posted - 2008-03-24 : 16:00:25
When you use the concatenate/addition opersator (+) with an integer involved, SQL assumes that you want to add the numbers together instead of concatenating the strings. In you example, it is trying to convert your textual information and, big surprise, is unable to. Cast the integer to a string and you should be good to go and, yes, it is a pain.

=======================================
Do something for somebody every day for which you do not get paid. -Albert Schweitzer, philosopher, physician, musician, Nobel laureate (1875-1965)
Go to Top of Page
   

- Advertisement -