简体   繁体   English

执行联接时SQL Server表出错

[英]Error on SQL Server tables while performing a join

I'm trying to perform a join between 4 tables schedule table, employee table, machine table and plate table. 我正在尝试在4个表计划表,员工表,机器表和板式表之间执行联接。

Problem is, at schedule table its primary key is a date while on the rest of the table have their primary key as char. 问题是,在计划表中,其主键是日期,而在表的其余部分,其主键为char。 I'm trying to convert the primary key in table into char to make it the same as the one on the others but I don't know how. 我正在尝试将表中的主键转换为char以使其与其他键相同,但是我不知道如何。 Here is what I have done so far 这是我到目前为止所做的

create table Machine
(
    MachineID char(5),
    MachineType varchar(10) NOT NULL,
    MachineStatus varchar(10)NOT NULL
)

create table Employee
(
    EmployeeID char(5) Primary Key,
    EmployeeName varchar(30) NOT NULL,
)

create table Plate
(
    PlateID char(5) Primary Key,
    PlateModel varchar(30) NOT NULL,
)

create table MachineSchedule
(
    DateSchedule date Primary Key,
)

here is my join 这是我的加入

SELECT Employee.EmployeName, Plate.PlateModel, Machine.MachineID, Machine.MachineType, Machine.MachineStatus
FROM Employee, Plate, Machine
JOIN MachineSchedule
ON MachineSchedule.DateSchedule=Machine.MachineID;

and here is the error it gave Conversion failed when converting date and/or time from character string. 这是从字符串转换日期和/或时间时,转换失败的错误

The table design and it's relation are not done properly. 表格设计及其关系未正确完成。 Based on this design and relation the simplest solution,i mean the query to make error free: 基于此设计和关系最简单的解决方案,我的意思是使查询没有错误:

DROP TABLE [MachineSchedule];

Create The table 创建表

CREATE TABLE [dbo].[MachineSchedule](
    [DateSchedule] [date] NOT NULL,
    [MachineID] [char](5) NULL,
PRIMARY KEY CLUSTERED 
(
    [DateSchedule] 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

Changed Query 变更查询

SELECT Employee.EmployeeName, Plate.PlateModel, Machine.MachineID, Machine.MachineType, Machine.MachineStatus
FROM Employee, Plate, Machine
JOIN MachineSchedule
ON MachineSchedule.MachineID = Machine.MachineID;

Your Table structure is incorrect, you should use integer datatype for primary key not char or datetime as its creates indexing and we can not join table on char and date datatype they never have same value; 您的表格结构不正确,您应为整数键(而不是char或datetime)使用整数数据类型,因为它会创建索引,并且我们无法将基于char和date数据类型的表连接在一起,因为它们的值永远不会相同; and lastly where is the join for other tables in from clause. 最后,from子句中其他表的联接在哪里。 there should be join condition for Employee, Plate, Machine. 员工,印版,机器应有加入条件。

and primary and foreign key constraints also missing 并且主键和外键约束也丢失了

You think Date column=char column ? 您认为日期列=字符列?

Use with conver to char For example 与conver一起使用以char例如

On cast(m.id as char(20))=ma.id Or update column type 在cast(m.id作为char(20))= ma.id或更新列类型

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM