我有两个存储相同数据的表。一个用于活动用户,另一个用于非活动用户。当用户来时,它在活动表中被搜索,如果没有找到,则在非活动表中搜索它。如果在非活动表中找到用户信息,则应将其移至活动表并从非活动表中删除。
表格中有一列存储照片。当我尝试将信息插入活动表时,出现以下错误:
SQLSTATE[22018]: [Microsoft][SQL Server Native Client 11.0][SQL Server]Operand type clash: nvarchar(max) is incompatible with image
我确定这是由照片引起的,因为如果用户信息没有照片,则移动成功。但是当有照片时,它会因上述错误而失败。
创建表的SQL:
CREATE TABLE [dbo].[tblBackup](
[Id] [int] IDENTITY(1,1) NOT NULL,
[DriverId] [int] NULL,
[FirstNameAmh] [nvarchar](100) NULL,
[FatherNameAmh] [nvarchar](100) NULL,
[GrandNameAmh] [nvarchar](100) NULL,
[Photo] [image] NULL
)
以下是代码:
$dbc->beginTransaction();
$sql = "select * from tblBackup where Id=?";
$stmt = $dbc->prepare($sql);
$stmt->bindParam(1, $_GET["gid"]);
$stmt->execute();
$row = $stmt->fetch();
$ins = "insert into tblActive(Id, DriverId, FirstNameAmh, FatherNameAmh, GrandNameAmh, Photo) values(?, ?, ?, ?, ?, ?)";
$st = $dbc->prepare($ins);
$val = array($row['Id'], $row['DriverId'], $row['FirstNameAmh'], $row['FatherNameAmh'], $row['GrandNameAmh'], $row['Photo']);
$st->execute($val);
$sql = "delete from tblBackup where Id=?";
$stmt = $dbc->prepare($sql);
$stmt->bindParam(1, $_GET["gid"]);
$stmt->execute();
$dbc->commit();
编辑:
我得出结论,PHP检索的照片数据被SQL服务器视为nvarchar(max)类型而不是图像类型。因此,SQL服务器抱怨它无法在图像数据类型列中插入nvarchar(max)。有办法解决这个问题吗?
答案 0 :(得分:0)
如果您处于开发阶段,我认为最好创建一个存储过程,包括“insert select”和“gid”作为参数并从php执行它。它不能成为您的解决方案吗?
答案 1 :(得分:0)
我稍微更改了代码,现在它正在运行。看来这个问题是由准备好的声明造成的,尽管我不明白为什么。
$dbc->beginTransaction();
$sql = "select * from tblBackup where Id=?";
$stmt = $dbc->prepare($sql);
$stmt->bindParam(1, $_GET["gid"]);
$stmt->execute();
$row = $stmt->fetch();
$ins = "insert into tblActive(Id, DriverId, FirstNameAmh, FatherNameAmh, GrandNameAmh, Photo) values(?, ?, ?, ?, ?, ".$dbc->quote($row['Photo']).")";
$st = $dbc->prepare($ins);
$val = array($row['Id'], $row['DriverId'], $row['FirstNameAmh'], $row['FatherNameAmh'], $row['GrandNameAmh']);
$st->execute($val);
$sql = "delete from tblBackup where Id=?";
$stmt = $dbc->prepare($sql);
$stmt->bindParam(1, $_GET["gid"]);
$stmt->execute();
$dbc->commit();