Check Constraint with Case When - sql-server

Trying to add a check constraint to the following table:
CREATE TABLE TEST_A
(
NAME VARCHAR(55),
Country VARCHAR(50)
)
ALTER TABLE TEST_A
ADD CONSTRAINT CK_GBR_TO_IND
CHECK (Country = CASE WHEN 'GBR' THEN 'IND' ELSE COUNTRY END);
I am getting the following error:
Msg 4145, Level 15, State 1, Line 2
An expression of non-boolean type specified in a context where a condition is expected, near 'THEN'.

Try a Trigger instead..You Can't use Check Constraint to change values...
create trigger trg_test
on yourtable
instead of insert
as
Begin
insert into yourtable--assuming it has only country column
select case when country='GBR' then 'IND'
else country end
from Inserted
end

ALTER TABLE TEST_A ADD CONSTRAINT CK_GBR_TO_IND
CHECK (Country IN('GBR', 'IND'));

Related

T-SQL crazy function result

I have this function:
CREATE FUNCTION CheckAkvNames (#Name VARCHAR(20))
RETURNS INT
AS
BEGIN
DECLARE #NoTexist int = 1
SELECT
#NoTexist = CASE WHEN COUNT(*) > 0 THEN 0 ELSE 1 END
FROM
[dbo].[Names]
WHERE
[Name] = #Name
RETURN #NoTexist
END
GO
ALTER TABLE [dbo].[Names]
ADD CONSTRAINT chkNames CHECK(dbo.CheckAkvNames([Name]) = 1);
GO
The problem is, when I run this on empty table I can't insert ...
So this change works:
CASE WHEN (COUNT(*) - 1) > 0 THEN 0 ELSE 1 END
WHY? Any ideas?
Edit:
Aim is to insert only names that are not in the table. I know it would be better to use key, point of the question is not to find better solution but why this solution does not work.
The constraint you added to the table actually means that you can't insert any name in the table, because for any value inserted in the table the function should return 1.This is impossible because if the name was inserted then the constraint would be violated.
This is why count(*) - 1 works: if there is already a name inserted and you tried to insert the same name then the constraint would be violated.
If you want unique names in a table, do not use a check constraint, use a unique constraint (or equivalently a unique index):
ALTER TABLE [dbo].[Names]
ADD CONSTRAINT unq_names_name UNIQUE (Name);

Temp tables, Column name or number of supplied values does not match table definition

Even though this tends to look as a duplicate, I had to post it as I can't seem to spot the error.
I don't know if I am mad or what but I can't seem to spot why there is a mismatch in the number of supplied values.
Here are they:
CREATE TABLE #TIPSTOPE_TS
(
TIP INT NULL,
SIFVAL VARCHAR(5),
GRUPA INT NULL,
DATUMOD VARCHAR(15),
PASIVNA DECIMAL(15,4) NULL DEFAULT(0),
REDOVNA DECIMAL(15,4) NULL DEFAULT(0),
ZATEZNA DECIMAL(15,4) NULL DEFAULT(0),
STOPA DECIMAL(15,4) NULL DEFAULT(0),
DATUMDO VARCHAR(15),
KONTO VARCHAR(15),
)
INSERT INTO #TIPSTOPE_TS
SELECT TS.TIP,
TS.SIFVAL,
TS.GRUPA,
CASE WHEN ISDATE(MAX(TS.DATUMOD)) = 0 THEN '2017.12.31' ELSE MAX(TS.DATUMOD) END AS DATUMOD,
CAST (2 AS DECIMAL(10,4)) AS PASIVNA,
CAST (1 AS DECIMAL(10,4)) AS REDOVNA,
CAST (3 AS DECIMAL(10,4)) AS ZATEZNA,
TS.REDOVNA,
TS.DATUMDO,
TP.M1 AS KONTO
FROM TIPSTOPE TS WITH(NOLOCK)
JOIN TIPPART TP WITH(NOLOCK) ON TP.TIP = TS.TIP
WHERE TS.DATUMOD <= '2017.12.31'
GROUP BY TS.TIP,TS.SIFVAL,TS.GRUPA,TP.M1,TS.DATUMDO,TS.REDOVNA
CREATE NONCLUSTERED INDEX IX_TIPSTOPE_TS ON #TIPSTOPE_TS (TIP, GRUPA, SIFVAL)
INCLUDE (DATUMOD)
And the second one...
CREATE TABLE #UNPVT_TIPSTOPE_TS
(
TIP INT NULL,
SIFVAL VARCHAR(5) NULL,
GRUPA INT NULL,
DATUMOD VARCHAR(10) NULL,
TIP_KS VARCHAR(15) NULL,
KAMATNA_STOPA DECIMAL(15,4) NULL DEFAULT(0),
DATUMDO VARCHAR(10) NULL,
)
INSERT INTO #UNPVT_TIPSOPE_TS
SELECT TIP, SIFVAL, GRUPA, DATUMOD, TIP_KS, KAMATNA_STOPA,DATUMDO
FROM
(
SELECT TIP, SIFVAL, GRUPA, DATUMOD, ISNULL(REDOVNA,0) AS REDOVNA, ISNULL(PASIVNA,0) AS PASIVNA, ISNULL(ZATEZNA,0) AS ZATEZNA,STOPA,DATUMDO
FROM #TIPSTOPE_TS
) P
UNPIVOT (KAMATNA_STOPA FOR TIP_KS IN (REDOVNA, PASIVNA, ZATEZNA)) AS UNPVT
The second temp tables is taking data from the first one.
When I try to create the second one error is thrown:
Insert error: Column name or number of supplied values does not match table definition
You are specifying the exact number of values that are needed. If you copy the whole code in new query window and execute it, it will work. Or in your current window drop the table table:
DROP TABLE #TIPSTOPE_TS;
DROP TABLE #UNPVT_TIPSTOPE_TS;
I mean execute only the above statements, and the execute the rest of the code. It should work again.
Sometime, when are debugging we forgot that the temporary table meta data is cached. For example, you can have the following code:
DROP TABLE IF EXISTS #TEST;
CREATE TABLE #TEST
(
[A] INT
);
INSERT INTO #TEST ([A])
SELECT 1;
And its valid. If we change it to this:
DROP TABLE IF EXISTS #TEST;
CREATE TABLE #TEST
(
[A] INT
,[B] INT
);
INSERT INTO #TEST ([A], [B])
SELECT 1, 2;
We will get:
Msg 207, Level 16, State 1, Line 9 Invalid column name 'B'.
Because, in the current session the #TEST table already exists and the engine is able to check that the B column does not exists. So, we need to drop the table manually, after the columns are changed, or we need to drop the tables at the end of our code statements.

Msg 207 Invalid column name $node_id for pseudo column in inline table valued function

In a Node-table the pseudo column name $node_id refers to the internal name of the node id column and using the pseudo column is recommended (see SQL Graph Architecture §Node Table).
For example after creating the following table:
create table [Sites](
[SiteName] NVarChar(max) NOT NULL,
[EndPoint] NVarChar(max),
[SiteNameHash] as CheckSum([SiteName]) PERSISTED NOT NULL,
[EndPointHash] as CheckSum([EndPoint]) PERSISTED NOT NULL,
INDEX IX_Sites_NodeId UNIQUE CLUSTERED ($node_id),
INDEX IX_Sites_SiteName UNIQUE NONCLUSTERED (SiteNameHash, $node_id),
INDEX IX_Sites_EndPoint UNIQUE NONCLUSTERED (EndPointHash, $node_id))
as Node;
The query:
SELECT $node_id
,[SiteName]
,[EndPoint]
,[SiteNameHash]
,[EndPointHash]
FROM [EmersonAnalysis].[dbo].[Sites]
where $node_id = '{"type":"node","schema":"dbo","table":"Sites","id":0}'
uses the pseudo column $node_id in the SELECT-clause and in the WHERE-clause to select a single node (if it exists).
However, on the following table valued function:
create function SitesByName(
#sitename as NVarChar(max))
RETURNS TABLE
WITH SCHEMABINDING
AS
return select
$node_id,
[SiteName],
[EndPoint],
[SiteNameHash],
[EndPointHash]
from [dbo].[Sites]
where [SiteNameHash] = CHECKSUM(#sitename) AND
[SiteName] = #sitename;
The similar query:
select
fn.$node_id
from [Sites]
outer apply SitesByName([SiteName]) as fn
Results in the error message:
Msg 207, Level 16, State 1, Line 2
Invalid column name '$node_id'.
Is it possible to use pseudo column names when selecting columns from functions? If so, how can I use pseudo column names?
PS. I'm using sql-server-2017 RC 2 v14.0.900.75.
Workarround: you can define an alias for $node_id in the table valued function and use that in your select instead of the pseudo column:
ALTER function [dbo].[SitesByName](
#sitename as NVarChar(max))
RETURNS TABLE
WITH SCHEMABINDING
AS
return select
$node_id [NodeId],
[SiteName],
[EndPoint],
[SiteNameHash],
[EndPointHash]
from [dbo].[Sites]
where [SiteNameHash] = CHECKSUM(#sitename) AND
[SiteName] = #sitename;
If fact SSMS displays a red squiggly line (likely an error or warning) under [SitesByName] with the text:
No column was specified for column '1' of 'SitesByName'

Set a check constraint for text array in Postgresql

I'm having a table called student, with id and name as fields in PostgreSQL:
Create table student (id int, name text[]);
I need to add the constraint for the name field. Which means it has to accept only character for that field. But the field name is a text array.
I tried this check constraint:
Alter table student
add constraint stud_const check (ALL(name) NOT LIKE '%[^a-zA-Z]%');
But it throws this error:
ERROR: syntax error atERROR: syntax error at or near "all"
LINE 1: ... student add constraint stud_const check (all(name) ...
or near "all"
How could I solve this problem? The constraint should be set to whole array.
It is necessary to unnest the array to match it to a regular expression:
select bool_and (n ~ '^[a-zA-Z]*$')
from unnest(array['John','Mary']) a(n)
;
bool_and
----------
t
bool_and. Since it is not possible to use a subquery in the check constraint wrap it in a function:
create function check_text_array_regex (
a text[], regex text
) returns boolean as $$
select bool_and (n ~ regex)
from unnest(a) s(n);
$$ language sql immutable;
and use the function in the check constraint:
create table student (
id serial,
name text[] check (check_text_array_regex (name, '^[a-zA-Z]*$'))
);
Test it:
insert into student (name) values (array['John', 'Mary']);
INSERT 0 1
insert into student (name) values (array['John', 'Mary2']);
ERROR: new row for relation "student" violates check constraint "student_name_check"
DETAIL: Failing row contains (2, {John,Mary2}).

PLS-00103: Encountered the symbol "end-of-file" Sql Developer

I want to execute following sql code.
--1
CREATE TABLE User_Type(
User_Type_ID Integer NOT NULL PRIMARY KEY,
User_Type_Name Char(20 ) NOT NULL
);
CREATE SEQUENCE User_Type_seq;
CREATE TRIGGER User_Type_ID_trg
BEFORE INSERT ON User_Type
FOR EACH ROW
BEGIN
SELECT User_Type_seq.NEXTVAL INTO :new.User_Type_ID
FROM DUAL;
END;
/
When I write it by hand to sql developer, it works. But I need a lot of block like this. So, I've edited those with notepad++ and copy/paste to sql developer again. This time; tables and sequences were created but it gives an error about trigger code (I've taken this routine with select from user_errors)
PLS-00103: Encountered the symbol "end-of-file" when expecting one of the following: ERROR 103
;
Here is the full code which doesn't work. create table and sequences codes works, but the trigger code doesn't.
--1
CREATE TABLE User_Type(
User_Type_ID Integer NOT NULL PRIMARY KEY,
User_Type_Name Char(20 ) NOT NULL
);
CREATE SEQUENCE User_Type_seq;
CREATE TRIGGER User_Type_ID_trg
BEFORE INSERT ON User_Type
FOR EACH ROW
BEGIN
SELECT User_Type_seq.NEXTVAL INTO :new.User_Type_ID
FROM DUAL;
END;
/
--2
CREATE TABLE Users(
User_ID Integer NOT NULL PRIMARY KEY,
User_Password Char(8 ) NOT NULL,
User_Name Char(20 ) NOT NULL,
User_Type_ID Integer NOT NULL
);
CREATE SEQUENCE Users_seq;
CREATE TRIGGER User_ID_trg
BEFORE INSERT ON Users
FOR EACH ROW
BEGIN
SELECT Users_seq.NEXTVAL INTO :new.User_ID
FROM DUAL;
END;
/
--3
CREATE TABLE Admin(
Admin_ID Integer NOT NULL PRIMARY KEY,
E_mail Char(20 ) NOT NULL,
Phone_Number Char(20 ) NOT NULL,
User_ID Integer NOT NULL
);
CREATE SEQUENCE Admin_seq;
CREATE TRIGGER Admin_ID_trg
BEFORE INSERT ON Admin
FOR EACH ROW
BEGIN
SELECT Admin_seq.NEXTVAL INTO :new.Admin_ID
FROM DUAL;
END;
/
--4
CREATE TABLE City(
City_ID Integer NOT NULL PRIMARY KEY,
City_Name Char(20 ) NOT NULL
);
CREATE SEQUENCE City_seq;
CREATE TRIGGER City_ID_trg
BEFORE INSERT ON City
FOR EACH ROW
BEGIN
SELECT City_seq.NEXTVAL INTO :new.City_ID
FROM DUAL;
/
I've searched similar questions but no response to solve my problem.
Sorry for English, thank you...
You're just missing an END; in your final trigger:
CREATE TRIGGER City_ID_trg
BEFORE INSERT ON City
FOR EACH ROW
BEGIN
SELECT City_seq.NEXTVAL INTO :new.City_ID
FROM DUAL;
END;
/
Everything else is already created and compiled successfully.
Incidentally, from 11g you can assign a sequence directly in PL/SQL, withouth having to select from dual:
...
BEGIN
:new.City_ID := City_seq.NEXTVAL;
END;
/
It was solved. Unfortunately, a letter mistake I have not understood yet why, but:
it must be
create trigger
instead of
CREATE TRIGGER

Resources