How to reference CLR UDTs when creating a table - sql

I have no idea why there is very little documentation on this so I'll ask here.
I have created some user defined data types and would like to use them when creating a table.
However I have no idea what the syntax is for calling to them.
Yes I know it's best to STAY AWAY from them but I'm in a position where I am forced to work with them.

You use User Defined Types (UDTs) just like the built-in types.
Defining UDT Tables and Columns
There is no special syntax for creating a UDT column in a table. You
can use the name of the UDT in a column definition as though it were
one of the intrinsic SQL Server data types.
The following CREATE TABLE Transact-SQL statement creates a table
named Points, with a column named ID, which is defined as an int
identity column and \ the primary key for the table. The second column
is named PointValue, with a data type of Point. The schema name used
in this example is dbo. Note that you must have the necessary
permissions to specify a schema name. If you omit the schema name, the
default schema for the database user is used.
CREATE TABLE dbo.Points
(
ID int IDENTITY(1,1) PRIMARY KEY,
PointValue Point
)
Registering User-Defined Types in SQL Server

Related

If not exist clause SQL statement

so I found this sql query in a project I am succeeding. This is the first time I encountering this clause/statement. I understand that this is to look if the table exist before creating one and that Object_ID is the table name that is to be created.
My questions are:
Does sysobject mean the database?
What is the Object property?
I know that it is not the columns inside the table to be created.
The columns are : dtb_color_id and description.
can someone explain this to me. please?
IF NOT EXISTS(SELECT * FROM SYSOBJECTS WHERE ID = OBJECT_ID('DTB_COLOR') AND OBJECTPROPERTY(ID,'ISUserTable') = 1)
BEGIN
.......some query I understand
END
sysobjects, OBJECTPROPERTY and OBJECT_ID are used in Microsoft SQL Server. They are part of the SQL Server DMVs and system functions/procedures used to query and manipulate the metadata.
sys.sysobjects is simply the list of all objects (tables, views, SPs, functions, etc) on the server in the active database. Please note, that sys.sysobjects is deprecated and is only available for backward compatibility. Use sys.objects instead
https://learn.microsoft.com/en-us/sql/relational-databases/system-dynamic-management-views/system-dynamic-management-views?view=sql-server-ver16
It has (as far as I know) no meaning in MySQL, unless somebody specifically created them.
You can also use INFORMATION_SCHEMA which is available in MySQL too (however slightly different in different RDBMS).
MSSQL INFORMATION_SCHEMA: https://learn.microsoft.com/en-us/sql/relational-databases/system-information-schema-views/system-information-schema-views-transact-sql?view=sql-server-ver16
MySQL INFORMATION_SCHEMA: https://dev.mysql.com/doc/refman/8.0/en/information-schema.html
SQL Server has no CREATE TABLE IF NOT EXISTS construct, a variation of the mentioned condition is commonly used to imitate that.
This is a way in SQL Server to check if a table exists in the active database and to perform actions according to the result, like creating the table.
OBJECTPROPERTY simply checks (in this case) if the table is a user created one.
https://learn.microsoft.com/en-us/sql/t-sql/functions/objectproperty-transact-sql?view=sql-server-ver16
I would remove the OBJECTPROPERTY condition in case the part you understand is a CREATE TABLE statement. You don't want to create a table which has a similar name to any system table/view, also you don't want to execute the CREATE TABLE if there is a VIEW with the same name (table creation will fail)
Yes sysobject means database.
The OBJECTPROPERTY() function returns information about schema-
scoped objects in the current database. Use this to check if an
object is a table, view, stored procedure, etc. You can also use
it to check if a table has a primary key, foreign key, foreign
key reference, etc.
For more details : https://learn.microsoft.com/en-us/sql/t-sql/functions/objectpropertyex-transact-sql?view=sql-server-ver16
In this scenario it is used to check whether it is user table or
not. The result of the ISUserTable property is 1 when it is user
table otherwise returns 0.
Here the following steps are followed:
First, it executes the select statement inside the IF Exists
If the select statement returns a value that condition is TRUE for IF Exists
It starts the code inside a begin statement
DTB_COLOR - May be a stored procedure

Cannot find data type user defined data type

I am getting Column, parameter, or variable #1: Cannot find data type dbo.SUBSYSTEM_CODE. error on user datatype.
CREATE TABLE #PREDEFINED_SUBSYSTEMS
(
SUBSYSTEM_CODE dbo.SUBSYSTEM_CODE PRIMARY KEY
);
After I checked user defined datatype I can see. I am using SQL 2012 and also I applied set compatibility_level = 110 on datatype still didn't work.
What other alternatives I have to fix this?
Probably you need to define type inside tempdb:
USE tempdb;
GO
CREATE TYPE dbo.SUBSYSTEM_CODE ...
Our problem was with schema
we moved table type from dbo to hub schema, and after that we got this error,
you need to use schema before table name for it to work
and dont forget about dynamic sqls which you might have

Default constraint with synonym or UDF from other database

Is it possible to add default constraint with synonym or UDF from other database?
Create table TestTable (
ID int identity(1,1),
SData varchar(100),
UserName varchar(100) default [OtherDatabaseName].dbo.fn_Test('value'))
Below is the error message, while I am trying to add default constraint
The name "OtherDatabaseName" is not permitted in this context. Valid
expressions are constants, constant expressions, and (in some
contexts) variables. Column names are not permitted.
We can add UDF from same database but I want add UDF from other database. Because it is used in multiple databases on same server.
And I do not want to create that UDF in all database and prevent duplicate code.
Please let me know if there is any other/better way. Thank you.
Please let me know if there is any other/better way.
One other way would be to use a trigger instead of a default.

SQL Server Table Schema Name

I have some questions about the schema in a table.
Sometime when you create a table the default schema is dbo.TableName. Is the dbo the default schema name? I believe you can change or specify the schema when creating a table right, because there are tables that have different schema like: Sales.Tablename or Users.Roles, etc. I believe the purpose of a schema is to make a difference between tables or something like that? Something like a namespace within a C# class. Is it possible to have two tables with same name but a different schema, like: Sales.Users, Marketing.Users ?
dbo is the default schema. You can change the default schema for each sql-login.
If you accidentally create a table in the wrong schema, you can move it:
-- Moving Peter table from Sales schema to Orders schema
ALTER SCHEMA Orders TRANSFER Sales.peter
You can specify which schema to create the table in by specifying it before the table name:
CREATE TABLE Sales.Users(id int);
One of the purposes of schemas is to create logical groups of tables, just like namespaces in C#. They are also useful for controlling permissions and more.
Yes, table names only need to be unique within each schema..
Sometime when you create a table the default schema is dbo.TableName. Is the dbo
the default schema name?
Why do you ask? It is quite obvious that dbo is the default schame name if you get it as default, or? On top it is the only usable schema a new database has.
I believe you can change or specify the schema when creating a table right,
What sense would multiple schemata have if you could not use them? And as the create table syntax clearly states you can specify a schema.
I believe the purpose of a schema is to make a difference between tables or
something like that? Something like a namespace within a C# class.
That pretty much sums it up.
Is it possible to have two tables with same name but a different schema,
What about you spend 10 seconds to try it out? Are you challenged by he concept of trying something totally simplistic out? And the answer is yes. object names have to be unique - within their schema.

Can you use "Drop table ......." in SSRS

I am creating a report which drops the table if it exists at the beginning and inserts the data that is needed.
The SQl runs smoothly in SQL Server Management Studio with no issues but when I place it into SSRS to create a report, it is saying that the synax is wrong.
I think I might need to do this as a stored procedure but I wanted to make sure that I have not missed anything first.
Thanks in advance.
Place the code in a Stored procedure, and use temporary tables or table variables rather.
Have a look at
CREATE TABLE (Transact-SQL)
Temporary Tables
You can create local and global temporary tables.
Local temporary tables are visible only in the current session, and
global temporary tables are visible to all sessions. Temporary tables
cannot be partitioned.
Prefix local temporary table names with single number sign
(#table_name), and prefix global temporary table names with a double
number sign (##table_name).
SQL statements reference the temporary table by using the value
specified for table_name in the CREATE TABLE statement
Also have a look at
DECLARE #local_variable (Transact-SQL)
#table_variable_name
Is the name of a variable of type table. Variable names must begin
with an at (#) sign and conform to the rules for identifiers.
Defines the table data type. The table declaration includes column
definitions, names, data types, and constraints. The only constraint
types allowed are PRIMARY KEY, UNIQUE, NULL, and CHECK. An alias data
type cannot be used as a column scalar data type if a rule or default
definition is bound to the type.
is a subset of information used to define a
table in CREATE TABLE. Elements and essential definitions are included
here. For more information, see CREATE TABLE (Transact-SQL).