gpt4 book ai didi

SQL Server 消息 241,级别 16,状态 1,从字符串转换日期和/或时间时转换失败

转载 作者:行者123 更新时间:2023-12-04 22:07:07 28 4
gpt4 key购买 nike

执行 stored procedure 时出现此错误我创造的

Msg 241, Level 16, State 1, Procedure UDP_INITIAL_CUSTOMER_DIM_POPULATION, Line 28
Conversion failed when converting date and/or time from character string.



该过程的代码是:
CREATE PROCEDURE UDP_INITIAL_CUSTOMER_DIM_POPULATION
AS
BEGIN
SET NOCOUNT ON

EXEC UDP_REMOVE_NULLS_FROM_CUSTOMER_STG

INSERT INTO dbo.customer_dim(customer_no, first_name, middle_name, last_name,
street_number, street_name, po_address, zip_code, city,
region, country)
SELECT DISTINCT
CAST(customer_no AS VARCHAR),
first_name,
middle_name,
last_name,
street_number,
street_name,
po_address,
zip_code,
city,
region,
country
FROM
dbo.customer_stg

PRINT 'Initial customer data in place!'
SET NOCOUNT OFF
END

以及创建 customer_dim 的代码表是:
CREATE PROCEDURE dbo.UDP_CREATE_CUSTOMER_DIM
AS
BEGIN
IF OBJECT_ID('customer_dim', 'U') IS NOT NULL
BEGIN
DROP TABLE customer_dim
END

CREATE TABLE [dbo].[customer_dim]
(
[customer_sk] INT IDENTITY (1,1) PRIMARY KEY,
[customer_no] [varchar](50) NULL,
[first_name] [varchar](50) NULL,
[middle_name] [varchar](50) NULL,
[last_name] [varchar](50) NULL,
[customer_name] [varchar](50) NULL,
[street_number] [varchar](50) NULL,
[street_name] [varchar](50) NULL,
[customer_address] [varchar](100) NULL,
[po_address] [varchar](50) NULL,
[zip_code] [varchar](50) NULL,
[city] [varchar](50) NULL,
[region] [varchar](50) NULL,
[country] [varchar](50) NULL,
[effective_date] DATE NULL DEFAULT GETDATE(),
[expiry_date] DATE NULL DEFAULT '999-12-31'
) ON [PRIMARY]
END

我曾尝试寻找解决方案,但因为我不想在日期或时间做任何事情,所以我找不到任何对我有意义的事情。

最佳答案

该错误是由 expiry_date 的默认值引起的。 .运行

SELECT CAST('999-12-31'AS DATE)

会产生一个错误:

Conversion failed when converting date and/or time from character string.



你想要的是
[expiry_date] DATE NULL DEFAULT '9999-12-31'

关于SQL Server 消息 241,级别 16,状态 1,从字符串转换日期和/或时间时转换失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36879088/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com