执行我创建的stored procedure
消息241,级别16,状态1,过程UDP_INITIAL_CUSTOMER_DIM_POPULATION,第28行
从字符串转换日期和/或时间时转换失败。
该程序的代码是:
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
我一直在寻找解决方案,但因为我没有在日期或时间做任何事情,所以我找不到任何对我有意义的事情。
答案 0 :(得分:4)
错误是由expiry_date
的默认值引起的。运行
SELECT CAST('999-12-31'AS DATE)
会产生错误:
从字符转换日期和/或时间时转换失败 字符串。
你想要的是
[expiry_date] DATE NULL DEFAULT '9999-12-31'
答案 1 :(得分:0)
默认值应由查询执行程序转换为列的数据类型。
日期数据类型需要"的 YYYY 强> -mm-DD" fromat
Luke问题与日期数据类型的默认值有关。
当sql尝试转换/转换yyy-mm-dd时,它无法将其转换为日期