存储过程 - 将数据类型varchar转换为float时出错

时间:2015-03-19 13:29:46

标签: sql-server stored-procedures

我试图将一个浮点值从一个表插入到另一个表中,但是我在这篇文章的主题行中得到了错误:

将数据类型varchar转换为float时出错。

两个表中的两个字段都设置为float。值是邮政编码的纬度和经度。纬度/经度的一个例子是:

45.895698 -72.365896

以下是我的代码的几个片段:

declare @v_latitude float
declare @v_longitude float

-- Grab Latitude & Longitude --
if @v_zip_code IS NOT NULL
SELECT @v_latitude=z_latitude, @v_longitude=z_longitude FROM tbl_ZipCodes WHERE z_zip_code = @v_zip_code

set @strValues = @strValues + '''' + @v_city + ''', ''' + @v_state + ''', ''' + @v_zip_code + ''', ''' + @v_daytime_phone + ''', ''' + @v_contact_name + ''', '''
+ @v_email_address + ''', '  + cast(@u_id as varchar) + ', ' +  cast(@d_id as varchar) + ', 1, ''' + cast(CURRENT_TIMESTAMP as varchar) + ''', 1 , ''' + cast(@v_latitude as float) + ''', ''' + cast(@v_longitude as float) + ''', '''
+ cast(CURRENT_TIMESTAMP as varchar) + ''', dft.df_t_id, (SELECT CASE WHEN dft.df_t_v_price <> 0 THEN dft.df_t_v_price WHEN dft.df_t_v_internet_price <> 0 THEN dft.df_t_v_internet_price WHEN dft.df_t_v_other_price <> 0 THEN dft.df_t_v_other_price ELSE 0 END AS v_search_price)'

使用@v_latitude和@v_longitude变量的代码部分位于:

... , ''' + cast(@v_latitude as float) + ''', ''' + cast(@v_longitude as float) + ''', ...

忽略“...”,因为我正在演示围绕这些值的附加代码。

任何帮助将不胜感激!

编辑:这是我正在使用的其他代码:

-- Check For User Profile --
SELECT @counter = COUNT(*) FROM tbl_Customers WHERE u_id = @u_id
if @counter > 0
    SELECT @v_city=c_city, @v_state =c_state, @v_zip_code=c_zip_code,@v_daytime_phone =c_phone_number FROM tbl_Customers WHERE u_id = @u_id
else
    SELECT @v_city=d_city, @v_state =d_state, @v_zip_code=d_zip_code,@v_daytime_phone =d_phone_number FROM tbl_Dealers WHERE d_id = @d_id

-- Grab Contact Information --
SELECT @v_contact_name = u_name, @v_email_address = u_email_address FROM tbl_Users WHERE u_id = @u_id


-- Grab Latitude & Longitude --
if @v_zip_code IS NOT NULL
    SELECT @v_latitude=z_latitude, @v_longitude=z_longitude FROM tbl_ZipCodes WHERE z_zip_code = @v_zip_code


-- Add Additional Fields --
set @strFields = @strFields + 'v_city, v_state, v_zip_code, v_daytime_phone, v_contact_name, v_email_address, u_id, d_id, v_processed, v_processed_date, v_active, ,v_latitude, v_longitude, v_last_activity, v_dealerfeed, v_search_price'
set @strJoin = @strJoin + ' LEFT JOIN tbl_Vehicles v ON v.v_stock_number = dft.df_t_v_stock_number'
set @strValues = @strValues + '''' + @v_city + ''', ''' + @v_state + ''', ''' + @v_zip_code + ''', ''' + @v_daytime_phone + ''', ''' + @v_contact_name + ''', '''
 + @v_email_address + ''', '  + cast(@u_id as varchar) + ', ' +  cast(@d_id as varchar) + ', 1, ''' + cast(CURRENT_TIMESTAMP as varchar) + ''', 1 , ''' + @v_latitude + ''', ''' + @v_longitude + ''', '''
 + cast(CURRENT_TIMESTAMP as varchar) + ''', dft.df_t_id, (SELECT CASE WHEN dft.df_t_v_price <> 0 THEN dft.df_t_v_price WHEN dft.df_t_v_internet_price <> 0 THEN dft.df_t_v_internet_price WHEN dft.df_t_v_other_price <> 0 THEN dft.df_t_v_other_price ELSE 0 END AS v_search_price)'

-- Insert Records Into Vehicle Table (but not twice!) --
set @strSQL = 'INSERT INTO tbl_Vehicles (' + @strFields + ') SELECT ' + @strValues + ' FROM tbl_DealerFeed_temp dft ' + @strJoin + ' WHERE dft.df_t_processed = 0 AND dft.df_d_id = ' +  cast(@df_d_id as varchar)
set @strSQL =  @strSQL + ' AND dft.df_t_v_stock_number NOT IN ( SELECT v.v_stock_number FROM tbl_Vehicles v WHERE v.d_id = ' + cast(@d_id as varchar) + ')'

print @strSQL

EXEC (@strSQL)

3 个答案:

答案 0 :(得分:2)

问题可能是你的动态sql在浮点数周围加上单引号。

尝试更改

 , ''' + cast(@v_latitude as float) + ''', ''' + cast(@v_longitude as float) + ''', ..

进入

 , ' + STR(@v_latitude,<length>,<decimals>) + ', ' + STR(@v_longitude,<length>,<decimals>) + ', ..

请参阅STR ()以获取参考资料

答案 1 :(得分:1)

当您正在执行动态SQL时,您需要将所有内容强制为字符串类型。

但它是绊倒你的单引号。如果您要插入的列是浮点数,则不希望插入的浮点值包含在引号中。

此外,我发现CONVERTCAST更灵活(主要是日期)(我只是为了多样性而做了CONVERT):

所以你想摆脱那些三重引号:

...+ CONVERT(varchar(20), @v_latitude) +',' + CONVERT(varchar(20), @v_longitude) +...

编辑添加:您的固定代码应如下所示:

set @strValues = @strValues + '''' + @v_city + ''', ''' + @v_state + ''', ''' + @v_zip_code + ''', ''' + @v_daytime_phone + ''', ''' + @v_contact_name + ''', '''
 + @v_email_address + ''', '  + cast(@u_id as varchar) + ', ' +  cast(@d_id as varchar) + ', 1, ''' + cast(CURRENT_TIMESTAMP as varchar) + ''', 1 , ' + Convert(varchar(20), @v_latitude) + ', ' + Convert(varchar(20), @v_longitude) + ', '''
 + cast(CURRENT_TIMESTAMP as varchar) + ''', dft.df_t_id, (SELECT CASE WHEN dft.df_t_v_price <> 0 THEN dft.df_t_v_price WHEN dft.df_t_v_internet_price <> 0 THEN dft.df_t_v_internet_price WHEN dft.df_t_v_other_price <> 0 THEN dft.df_t_v_other_price ELSE 0 END AS v_search_price)'

在没有 EXEC (@strSQL)的情况下执行SQL 并查看打印的SQL

再次编辑;修复你的Float被限制为四舍五入到4位小数(这取决于它是一个浮点数)但是你可以通过先将其转换为小数或使用STR来解决它,这个&#39;帮助你明白发生了什么:

declare @v_latitude float = -45.12345678
print @v_latitude
print 'Cast = '+ cast( @v_latitude as varchar)
print 'Convert = '+ convert(varchar(20), @v_latitude)
print 'Cast via decimal = '+ cast( cast(@v_latitude as decimal(18,9)) as varchar)
print 'Convert via decimal = '+ convert( varchar(20), convert(decimal(18,9), @v_latitude))
print 'str = '+ str(@v_latitude,18,9)

==
-45.1235
Cast = -45.1235
Convert = -45.1235
Cast via decimal = -45.123456780
Convert via decimal = -45.123456780
str =  -45.123456780

答案 2 :(得分:0)

你可以尝试

 ''' + cast( @v_latitude  as varchar(20))
 + ''', ''' + cast((@v_longitude as varchar(20))+ '''