PCSgtL
PCSgtL

Reputation: 262

T Sql Column only fills with null

Our team had an issue where a stored procedure did not fill every column with data. The stored procedure fills table from a view. We manually ran select statements from the view and the expected data was there. However when we ran Insert statements three columns in the middle of the table where null filled.

 Insert into Table2
 Select * from View1

Did not fill specific columns. We tested several times. The columns not filled where Table2.Num1,Table2.Num2,Table2.Num3. All values for these fields where null.

Select * from View1

All the data was present. View1.Num1, View1.Num2, View1.Num3. These where nvarchar(16) fields filled with numeric data.

Truncate Table2
Insert into Table2
Select * from View1

The same specific columns where not filling. The rest of the table did fine.

Created Table3 based on Table2.

Insert into Table3
Select * from View1

Worked fine, all the data was there.

Does someone know why an instance like this might happen?

Extra info requested

The table structure between the two tables is the same.

The specific columns having an issue are varchar(16)

What does created table3 mean?

We generated Create table2 statement, then renamed it to Table3 and executed it. Table3 is identical to table2.

Table structure

This is the statement used to create table2. The identical create statement is used for table3 except the table name is changed. The view has the same number of columns with the same names and definitions.

CREATE TABLE table2(
[emp_id] [int] NOT NULL,
[company] [char](4) NULL,
[empno] [int] NOT NULL,
[termination_date] [datetime] NULL,
[rehire_date] [datetime] NULL,
[hire_date] [datetime] NULL,
[first_name] [varchar](128) NULL,
[last_name] [varchar](128) NULL,
[middle_name] [varchar](128) NULL,
[aka] [varchar](128) NULL,
[soc_sec_no] [char](9) NULL,
[birth_date] [datetime] NULL,
[sex] [char](1) NULL,
[race] [char](1) NULL,
[marital_status] [char](1) NULL,
[disability] [char](1) NULL,
[citizenship] [char](4) NULL,
[military] [char](1) NULL,
[union_code] [char](8) NULL,
[eeo_loc] [varchar](254) NULL,
[pers_status] [char](1) NULL,
[employ_type] [char](2) NULL,
[normal_hours] [decimal](12, 2) NULL,
[week_over_hours] [decimal](12, 2) NULL,
[adj_service_date] [datetime] NULL,
[shift] [char](8) NULL,
[visa_number] [char](30) NULL,
[visa_exp_date] [datetime] NULL,
[passport_number] [char](30) NULL,
[passport_exp_date] [datetime] NULL,
[process_cobra] [char](1) NULL,
[maiden_name] [varchar](128) NULL,
[Visa ID] [varchar](15) NULL,
[Visa Type Name] [varchar](16) NOT NULL,
[TA_Source] [varchar](1) NOT NULL,
[LW_ID] [varchar](15) NULL,
[LW_ID_Type] [varchar](18) NOT NULL,
[Taleo_ID] [varchar](254) NULL,
[Uses_Tobacco] [varchar](3) NOT NULL,
[Source_DB] [varchar](8) NOT NULL,
[UserID] [varchar](254) NULL,
[Num1] [varchar](16) NULL,
[Num2] [varchar](16) NULL,
[Num3] [varchar](16) NULL,
[AFF_EmpID] [int] NULL,
[MHC_EmpID] [int] NULL,
[daily_hours] [decimal](12, 2) NULL,
[Orig_Hire_Date] [datetime] NULL,
[WD_Continuous_Service_Date] [datetime] NULL,
[Hire_Date_PM] [datetime] NULL,
[CompanyServiceDate] [datetime] NULL,
[Prim_Order] [bigint] NULL,
[AD_Domain] [varchar](128) NULL,
PRIMARY KEY CLUSTERED 
(
[empno] ASC
)
) ON [PRIMARY]

GO

Upvotes: 1

Views: 180

Answers (1)

Brad
Brad

Reputation: 12255

You should really be explicitly stating what columns go where and definitely not using a Select * on an Insert

 Insert into Table2(column1, column2, column3)
 Select column1, column2, column3
 from Table1

now you KNOW Table1.Column1 maps to Table2.Column2

Upvotes: 3

Related Questions