简体   繁体   English

将varchar值转换为数据类型int和JOIN两个表时,MS SQL转换失败

[英]MS SQL Conversion failed when converting the varchar value to data type int and JOIN two Tables

I get an error saying: "Conversion failed when converting the varchar to data type int " when I try to run the code below, but I already converted the ASSIGNED_ID to int in php ? 当我尝试运行以下代码时,我收到一条错误消息: “将varchar转换为int数据类型时转换失败” ,但是我已经在php中将ASSIGNED_ID转换为int了?

The $row["ID"] number is small its 2 so there are no , or anything like that, I even added a replace code to remove any spaces, but it still doesn't work. $row["ID"]数字小于2,所以没有或类似的东西,我什至添加了替换代码来删除任何空格,但仍然无法正常工作。 Also both tables have the same ID (data type: int) 另外,两个表具有相同的ID(数据类型:int)

I would also like to know how I can join the two tables: T_ASSIGNED and T_EQ using their ID (int) they both have it and it always matches 我还想知道如何连接两个表: T_ASSIGNEDT_EQ使用它们的ID (int)它们都具有它并且总是匹配

// SEND A SELECT QUERY TO MSSQL
$query = mssql_query('SELECT ID FROM [TestT].[dbo].[T_ASSIGNED] WHERE [A_T_N]=\'$contact_lastname\'');
$row = mssql_fetch_array($query);

if (strlen($row["ID"])<>0) {
// REMOVE ANY SPACES AND CONVERT ID TO INT
$ASSIGNED_ID = intval(str_replace(" ", "", $row["ID"]));
}else{
// IF NO ID IS FOUND MAKE ID BE ZERO
$ASSIGNED_ID =intval('0');
};


// FREE THE QUERY RESULT
mssql_free_result($query);

// ECHO THE ID
ECHO("TEST ID: ".$ASSIGNED_ID);

// GET THE T NUMBER IN THE T_EQ LOCATION
$query = mssql_query('SELECT EQ_T_N FROM [TestT].[dbo].[T_EQ] WHERE [ID]=\'$ASSIGNED_ID\'');
$row = mssql_fetch_array($query);

// ECHO THE EQ_T_N
echo(">".$row["EQ_T_N"]."<");

// FREE THE QUERY RESULT
mssql_free_result($query);

THANKS FOR ANY HELP. 谢谢你的帮助。

use this 用这个

mssql_query('SELECT EQ_T_N FROM [TestT].[dbo].[T_EQ] WHERE [ID]="' . $ASSIGNED_ID .'"');

by using single quotes, you're using the text of $ASSIGNED_ID , not the value of that variable 通过使用单引号,您使用的是$ASSIGNED_ID的文本,而不是该变量的值

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将varchar值&#39;[field]&#39;转换为数据类型int时转换失败 - Conversion failed when converting the varchar value '[field]' to data type int 我怎么了 将varchar转换为int数据类型时转换失败 - What is my issue? Conversion failed when converting the varchar to data type int 转换varchar值错误时转换失败 - Conversion failed when converting the varchar value error 将nvarchar值&#39;10,12&#39;转换为数据类型int时转换失败 - Conversion failed when converting the nvarchar value '10,12' to data type int Laravel为什么说“将nvarchar值&#39;[]&#39;转换为数据类型int时转换失败” - Why is Laravel saying “Conversion failed when converting the nvarchar value '[]' to data type int” PHP查询返回“转换varchar值时转换失败” - PHP Query returns 'Conversion failed when converting the varchar value' 使用 PHP 和 PDO 与 MS SQL 从字符串转换日期时间时转换失败 - Conversion failed when converting datetime from character string using PHP and PDO with MS SQL 带有 SQL Server 2008 的 Laravel 抛出“将 varchar 数据类型转换为日期时间数据类型导致值超出范围” - Laravel with SQL server 2008 throw “Conversion of a varchar data type to a datetime data type resulted in an out-of-range value” 更新 SQL 表将 varchar 转换为 int - Updating SQL table converting varchar to int 两个sql表的join - join of two sql tables
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM