SQL Server – How to Create and Fill Temp Table

sql serverstored-procedurestemporary-tables

I have a stored procedure that selects some data and returns it. Now I also want to add a temp table, fill it with data and return it with the stored procedure.

I define it as follows:

create table #tempDateTable
(
    Name varchar,
    TempDate datetime
)

INSERT INTO #tempDateTable(Name, TempDate) values ('Smith', @myDate)
SELECT * FROM #tempDateTable

@myDate is parameter passed to the stored procedure.

When I execute the stored procedure it gives me:

Msg 8152, Level 16, State 14, Procedure date_samples_procedure, Line 90
String or binary data would be truncated.

What I find strange is that Line 90 is not any of the above shown lines, but if I remove all the code for this #tempDateTable, the stored procedure works normally.

Best Answer

You should specify the length of the varchar column e.g. varchar(2000).