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.
Related
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
In HSLQDB v 2.3.1 there is a create type clause for defining UDTs. But there appears to be no alter type clause, as far as the docs are concerned (and the db returns a unexpected token error if I try this).
Is it possible to amend/drop a UDT in HSQLDB? What would be the best practice, if for example I originally created
create type CURRENCY_ID as char(3)
because I decide I'm going to use ISO codes. But then I actually decide that I'm going to store the codes as integers instead. What is the best way to modify the schema in my db? (this is a synthetic example, obviously I wouldn't use integers in this case).
I guess I might do
alter table inventory alter column ccy set data type int
drop type CURRENCY_ID
create type CURRENCY_ID as int
alter table inventory alter column ccy set data type CURRENCY_ID
but is there a better way to do this?
After trying various methods, I ended up writing a script to edit the *.script file of the database directly. It's a plain text file with SQL commands that recreates the DB programmatically. In detail:
open db, shutdown compact
Edit the script file: replace the type definition, e.g. create type XXX as int to create type XXX as char(4)
For each table, replace the insert into table XXX values (i,...) with insert into table XXX values('str',...). This was done with a script that had the mappings from the old (int) value into the new (char) value.
In my particular case, I was changing a primary key, so I had to remove the identity directive from the create table statement, and also I had to remove a line that had a alter table XXX alter column YYY restart sequence 123.
save and close script file, open db, shutdown compact
This isn't great, but it worked. Advantages :
Ability to re-define UDT.
Ability to map the table values programmatically.
Method is generic and can be used for other schema changes, beside UDTs.
Cons
No checking that schema is consistent (although it does throw up errors if it can't read the script).
Dangerous when reading file as a text file. e.g. what if I have a VARCHAR column with newlines in it? When I parse the script file and write it back, this would need to be escaped.
Not sure if this works with non-memory DBs. i.e. those that don't only have a *.script file when shutdown.
Probably not efficient for large DBs. My DB was small ~ 1MB.
I am using Power Designer to create a database model. In one of my tables, I have created a Check constraint that calls a function to validate the attribute. The script for my table creation looks like this
create table tbl_Inventory (
Id int identity,
Name VARCHAR(50) not null
constraint CK_Inventory_Name check (([dbo].[fn_CheckNameComplexLogic]([Name]) = 1)),
constraint PK_Inventory primary key (Id),
)
I have also created a function fn_CheckNameComplexLogic that performs the check.
When I try to use the code generation tool by going Database->Generate Database. The generated code always place create table before create function. Because my table depends on the function, the scripts always errors out. I could manually edit the generated code, but I am wondering if there is a place in PowerDesigner for configuring this.
Thanks for your help.
If may be a little overkill, but you can change the Generation Order to put (all) the functions (included in the Procedure category) before the Table.
embed the DBMS in your model with Database>Change Current DBMS
edit it with Database>Edit Current DBMS
change the order in the Script\Objects\GenerationOrder item (using the XML view)
put Procedure above Table
Simply add condition into Additional Checks of column properties tab after all constructs like %MINMAX% and %LISTVAL% and %CASE% and %RULES% and write here your condition ...
I need a very simple thing in SQL Database - I am using SQL Server and/or SQL Compact.
In c# I would write it like this
public class MyApp
{
public static int Version = 1;
}
e.g. I need to store configuration information in a form of singleton in SQL database.
Is there any better method than to create table with only one record?
Actually for my present needs it would be sufficient to have only one version number stored with database, but it must work both for SQL Server and SQL Compact database.
A table with one row is probably your best approach. Normally, you'd use a CHECK() constraint to guarantee you'll have only one row.
create table your_table_name (
one_row integer not null unique
default 1 check (one_row = 1),
version varchar(5) not null unique
);
insert into your_table_name values (1, '0.0.0');
If your platform doesn't support CHECK() constraints, but does support GRANT and REVOKE, you might be able to insert one row into the version table, then grant only update permissions. (Revoke delete and insert permissions.)
If your platform doesn't support CHECK() constraints, and doesn't support GRANT and REVOKE, but does support foreign key references, you might be able to replace the CHECK() constraint above with a foreign key reference to single-row table. This doesn't entirely solve the problem--you still have a single-row table that you can't adequately constrain.
If your dbms supports regular expressions in CHECK() constraints, you could add additional constraints to guarantee your version number follows a regular expression. You could also split the "version" into several columns of integers, each with its own constraints. But varchar(5) and varchar(7) seem to be the most common.
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