SQL SERVER: Defining the Same Database for a Temporary Table

I have a script in SQL Server. In this script, I create a temporary table like this:

CREATE TABLE #tmpTbl(ID char(36) NOT NULL, Field1 char(36) NOT NULL, Field2 varchar(50) NOT NULL, Field3 varchar(50))

      

to set sorting for the whole temporary table:

CREATE TABLE #tmpTbl(ID char(36) collate Modern_Spanish_CI_AS NOT NULL , Field1 char(36) collate Modern_Spanish_CI_AS NOT NULL , Field2 varchar(50) collate Modern_Spanish_CI_AS NOT NULL , Field3 varchar(50) collate Modern_Spanish_CI_AS)

      

As you can see, I am applying a mapping for each column. So I am trying to do the following:

  • Instead of applying a sort to every column of the temporary table, is it possible to apply the sort to all columns of the temporary table in onces?
  • Instead of manually specifying the mapping to Modern_Spanish_CI_AS that's in the database where the script is running, is there a way to get the mapping from the database via a variable and then set it to temporary columns?

for example in this latter case:

DECLARE @Collation = getdatabasecollation  --> Get current database collation where script is executed.

      

and then:

CREATE TABLE #tmpTbl(ID char(36) collate @Collation NOT NULL , Field1 char(36) collate @Collation NOT NULL, Field2 varchar(50) collate @Collation NOT NULL , Field3 varchar(50) collate @Collation)

      

or I said in point 1, set this collation to the whole temp table.

+3


source to share


1 answer


Just use COLLATE DATABASE_DEFAULT



CREATE TABLE #tmpTbl
  (
     ID     CHAR(36) COLLATE DATABASE_DEFAULT NOT NULL,
     Field1 CHAR(36) COLLATE DATABASE_DEFAULT NOT NULL,
     Field2 VARCHAR(50) COLLATE DATABASE_DEFAULT NOT NULL,
     Field3 VARCHAR(50) COLLATE DATABASE_DEFAULT NULL
  ) 

      

+3


source







All Articles