Reputation: 1013
I have a stored procedure I'm trying to create to fill a temporary table. But I need to make several passes at adding data based upon some conditions and capture those conditions with an additional field added to the temp table.
I start out like this:
select top 0
into #mytable
from UserTable
This I have found simply copies the basic structure with the same columns and types. Then I need to add a field:
alter table #mytable ADD reasontype varchar
The I make several passes at examining the table, here is one of them:
insert into #mytable
select distinct a.*, 'Annual'
from UserTable a
where (a.EnrollmentDate < DATEADD(year, -1, getdate())
This is to select those that require an annual review. The procedure compiles without an error but when I try to fill a datatable I get the error that string or binary data would be truncated. What am I doing wrong?
Upvotes: 0
Views: 58
Reputation: 4603
Use a length on your varchar, 'Annual' is being truncated to 'A'. If you're planning on indexing the reasontype field, don't use varchar(max), it exceeds the maximum width of an indexable field
Upvotes: 0
Reputation: 27904
alter table #mytable ADD reasontype varchar(max)
If that works, either use "max" or a value that won't truncate your values...or use a LEFT statement where the length matches the longest value of reasontype
example for left:
alter table #mytable ADD reasontype varchar(3)
insert into #mytable
select distinct a.*, LEFT('Annual',3)
from UserTable a
where (a.EnrollmentDate < DATEADD(year, -1, getdate())
but you probably just want this:
alter table #mytable ADD reasontype varchar(6) /* where 6 is the length of the string "Annual" */
Upvotes: 1