问题
When I run the following query in SQL Server 2014 to create a temporary table:
CREATE TABLE #temp
(
location char(16) null,
location_desc varchar(25) null,
Emp_Num EMPNUM null
)
I receive the error:
Cannot find the data type empnum.
empnum
is a user-defined data type. But the same user-defined data type is working fine in other stored procedures.
Why am I receiving this error?
回答1:
It looks like user defined types are defined at database level
, not instance level
, so they must be defined for each database. For temporary tables
, that means tempdb
- creation query should be something like the following:
exec tempdb.sys.sp_executesql N'CREATE type EMPNUM ...';
A discussion regarding this subject can be found here.
回答2:
If using the tempdb, you need to define the type in the model
database since types are at the db level. If the type has been defined for the model
then you may still have permissions issues if the model
db hasn't been given the db_ddladmin
role.
The model
db is a template for all new db's, tempdb
does not persist between restarts, it is created new each startup.
来源:https://stackoverflow.com/questions/36003051/cannot-find-user-defined-datatype-empnum-in-sql-server-2014