What is wrong with this access query? - sql

Running the below query returns 0 records, but I would expect it to return 3.
SELECT
ID,
DW2_TV_DimStation_Id,
DW2_OTT_DimStation_Id,
Name,
CoreTVCode,
CoreOTTCode,
StrataTVCode,
HouseHolds,
MaleSkew,
FemaleSkew,
AverageAge,
AverageIncome,
BroadReach,
Description,
Owner,
Notes,
timestamp,
CreatedOn,
ModifiedOn,
Retired,
1 AS Accepted
FROM
Planning_DimStation AS src
WHERE
src.[timestamp] = (
SELECT
MAX([timestamp])
FROM
Planning_DimStation AS src2
WHERE
src2.[ID] = src.[ID]
)
AND NOT EXISTS (
SELECT
1
FROM
DimStation AS tgt
WHERE
tgt.[ID] = src.[ID]
);
The part that breaks it is the NOT EXISTS statement. If I delete the NOT EXISTS it works fine.
Table 1: Planning_DimStation
Is an SQL table linked with 3 records in it. Source below.
Table 2: DimStation
Is an Access table (pic of source UI below) that is empty
Could this be a silent fail caused by type missmatch?
Table 1:
CREATE TABLE [Planning].[DimStation]
(
[ID] INT PRIMARY KEY,
[DW2_TV_DimStation_Id] INT NULL,
[DW2_OTT_DimStation_Id] INT NULL,
[Name] NVARCHAR(128) NOT NULL,
[CoreTVCode] CHAR(5) NULL,
[CoreOTTCode] CHAR(10) NULL,
[StrataTVCode] CHAR(10) NULL,
[HouseHolds] DECIMAL(5,2) NULL,
[MaleSkew] DECIMAL(5,2) NULL,
[FemaleSkew] DECIMAL(5,2) NULL,
[AverageAge] INT NULL,
[AverageIncome] DECIMAL(23,2) NULL,
[BroadReach] BIT NULL,
[Description] NVARCHAR(MAX) NULL,
[Owner] NVARCHAR(128) NULL,
[Notes] NVARCHAR(MAX) NULL,
[timestamp] timestamp NOT NULL,
[CreatedOn] DATETIME2(7) CONSTRAINT [df_Planning_DimStation_CreatedOn] DEFAULT (sysutcdatetime()) NOT NULL,
[ModifiedOn] DATETIME2(7) CONSTRAINT [df_Planning_DimStation_ModifiedOn] DEFAULT (sysutcdatetime()) NOT NULL,
[Retired] BIT CONSTRAINT [df_Planning_DimStation_Retired] DEFAULT (0) NOT NULL
)
GO
Table 2:

Joining on different data types tends to yield unexpected results.
To fix this, use casts.
A note is that Access doesn't allow nulls to be cast. So we need to work around that using Nz (same as ISNULL in T-SQL) and explicitly handling nulls.
AND NOT EXISTS (
SELECT
1
FROM
DimStation AS tgt
WHERE
CLng(IIF(tgt.[ID] IS NULL, 0, tgt.ID)) = src.[ID] AND NOT tgt.ID IS NULL
);

Related

Self-Referencing Table SQL Select last element from included elements

I have tables:
create table Branches(
Id int primary key identity,
IdSection int not null,
IdMasterBranch int null,
[Name] varchar(100) not null,
[Description] varchar(300) null
)
create table Threads(
Id int primary key identity,
IdBranch int not null,
IdUserAuthor int not null,
[Name] varchar(100) not null,
IsPinned bit not null,
IsOpen bit not null,
HasPoll bit not null
)
create table Posts(
Id int primary key identity,
IdThread int not null,
IdUserAuthor int not null,
Body varchar(4000) not null,
IsEdited bit not null,
[Index] int not null,
Created datetime2 not null,
Updated datetime2 null
)
Branches is self referencing table, that contains another branches.
And i wanna write sql request to select all master branches (IdMasterBranch IS NULL) with last thread and last post.
Something like this:
select * from Branches branch
left join BranchModerators moderator on branch.Id = moderator.IdBranch
left join Threads thread on branch.Id = thread.IdBranch
left join (select * from (
select *,
ROW_NUMBER() over (
partition by IdThread
order by [Index] desc
) as RowNumber
from Posts
) groups
where groups.RowNumber = 1) lastPost on thread.Id = lastPost.IdThread
But i wanna take last thread and post with included inner branches too.
What is the best solution for it?

Make a column required if other column is of certain value

What I have:
CREATE TABLE [dbo].[User]
(
[id] INT NOT NULL PRIMARY KEY,
[name] VARCHAR(50) NOT NULL,
[postcode] INT NOT NULL,
[phone] INT NULL
)
What I want is that the phone number is required ONLY if the postcode is higher than 40000. If postcode is smaller than 40000, user can insert the phone number, although it is not required.
How do I do this?
You can use a check constraint:
CREATE TABLE [dbo].[User]
(
[id] INT NOT NULL PRIMARY KEY,
[name] VARCHAR(50) NOT NULL,
[postcode] INT NOT NULL,
[phone] INT NULL,
CONSTRAINT CHK_Postcode CHECK (postcode >= 4000 OR Phone IS NOT NULL)
);
This needs to be handled from the front end inserting values into the Database. Insert Query on one column based on another in the DB for the same table is not possible.

How to save auto generated primary key Id in foreign key column in same table

Following is the table structure:
CREATE TABLE [User] (
[Id] bigint identity(1,1) not null,
[FirstName] nvarchar(100) not null,
[LastName] nvarchar(100) not null,
[Title] nvarchar(5) null,
[UserName] nvarchar(100) not null,
[Password] nvarchar(100) not null,
[Inactive] bit null,
[Created] Datetime not null,
[Creator] bigint not null,
[Modified] DateTime null,
[Modifier] bigint null
CONSTRAINT [PK_User] PRIMARY KEY CLUSTERED
(
[Id] Asc
)
);
IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[FK_User_Creator]') AND parent_object_id = OBJECT_ID(N'[User]'))
ALTER TABLE [User] ADD CONSTRAINT [FK_User_Creator] FOREIGN KEY([Creator]) REFERENCES [User]([Id])
GO
INSERT INTO [User] (Creator) Values ([Id] ?)
This is a case when table is empty and first user is going to add in table. Otherwise I don't have issue.
How can I insert Id in creator column with insert statement at the same time?
One way could be using Sequence instead of identity column. The below script might serve the same purpose:
CREATE SEQUENCE dbo.useridsequence
AS int
START WITH 1
INCREMENT BY 1 ;
GO
CREATE TABLE [User] (
[Id] bigint DEFAULT (NEXT VALUE FOR dbo.useridsequence) ,
[FirstName] nvarchar(100) not null,
[LastName] nvarchar(100) not null,
[Title] nvarchar(5) null,
[UserName] nvarchar(100) not null,
[Password] nvarchar(100) not null,
[Inactive] bit null,
[Created] Datetime not null,
[Creator] bigint DEFAULT NEXT VALUE FOR dbo.useridsequence ,
[Modified] DateTime null,
[Modifier] bigint null
CONSTRAINT [PK_User] PRIMARY KEY CLUSTERED
(
[Id] Asc
)
);
IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[FK_User_Creator]') AND parent_object_id = OBJECT_ID(N'[User]'))
ALTER TABLE [User] ADD CONSTRAINT [FK_User_Creator] FOREIGN KEY([Creator]) REFERENCES [User]([Id])
GO
INSERT INTO [User]
(
-- Id -- this column value is auto-generated
FirstName,
LastName,
Title,
UserName,
[Password],
Inactive,
Created,
Creator,
Modified,
Modifier
)
VALUES
(
'Foo',
'Bar',
'Title',
'UserName ',
'Password',
0,
GETDATE(),
DEFAULT,
GETDATE(),
1
)
SELECT * FROM [User] AS u
Result :
The short answer is that you can't do this. And I suggest your model is logically flawed in the first place. Do you intend to define all actual database users (e.g., create user ... for login ...) as rows in [Users]? You need to think about that - but the typical answer is no. If the answer is yes, then you don't need the creator column at all because it is redundant. All you need is the created date - for which you probably should have defined a default.
But if you want to do this, you will need to do it in two steps (and you will need to make the column nullable). You insert a row (or rows) with values for the "real" data columns. Then update those same rows with the identity values generated for id. An example showing different ways to do this
use tempdb;
set nocount on;
CREATE TABLE dbo.[user] (
[user_id] smallint identity(3,10) not null primary key,
[name] nvarchar(20) not null,
[active] bit not null default (1),
[created] Datetime not null default (current_timestamp),
[creator] smallint null
);
ALTER TABLE dbo.[user] ADD CONSTRAINT [fk_user] FOREIGN KEY(creator) REFERENCES dbo.[user](user_id);
GO
-- add first row
insert dbo.[user] (name) values ('test');
update dbo.[user] set creator = SCOPE_IDENTITY() where user_id = SCOPE_IDENTITY()
-- add two more rows
declare #ids table (user_id smallint not null);
insert dbo.[user] (name) output inserted.user_id into #ids
values ('nerk'), ('pom');
update t1 set creator = t1.user_id
from #ids as newrows inner join dbo.[user] as t1 on newrows.user_id = t1.user_id;
select * from dbo.[user] order by user_id;
-- mess things up a bit
delete dbo.[user] where name = 'pom';
-- create an error, consume an identity value
insert dbo.[user](name) values (null);
-- add 2 morerows
delete #ids;
insert dbo.[user] (name) output inserted.user_id into #ids
values ('nerk'), ('pom');
update t1 set creator = t1.user_id
from #ids as newrows inner join dbo.[user] as t1 on newrows.user_id = t1.user_id;
select * from dbo.[user] order by user_id;
drop table dbo.[user];
And I changed the identity specification to demonstrate something few developers realize. It isn't always defined as (1,1) and the next inserted value can jump for many reasons - errors and caching/restarts for example. Lastly, I think you will regret naming a table with a reserved word since references to it will require the use of delimiters. Reduce the pain.

Can one table have two identity columns in SQL Server?

I am trying to make two columns auto increment but this column shows an error [user_id] as id + 0 PRIMARY KEY NOT NULL saying
Only UNIQUE or PRIMARY KEY constraints can be created on computed columns
What I am trying to do is, if id = 1, make user_id= 1 as well.
CREATE TABLE [dbo.TBL_TXN_USER]
(
[id] int NOT NULL IDENTITY(1,1),
[user_id] as id + 0 PRIMARY KEY NOT NULL ,
[username] varchar(150) NOT NULL,
[fullname] varchar(150) NOT NUll,
[pwd] varchar(50) NOT NUll,
[email] varchar(150) NOT NULL,
[mobile] varchar(150) NOT NULL,
[designation] varchar(150) NOT NULL,
[deleted] int NULL,
[created_date] datetime NULL,
[creator_user_id] int NULL,
[changed_date] datetime NULL,
[changer_user_id] int NULL,
[add_content] int NULL,
[edit_content] int NULL,
[delete_content] int NULL,
[manage_user] int NULL,
[view_log] int NULL,
)
What is wrong in [user_id]? How to solve it?
the error message is because you put the NOT NULL constraint on the computed column.
on sql server 2012 the complete error message is:
Only UNIQUE or PRIMARY KEY constraints can be created on computed
columns, while CHECK, FOREIGN KEY, and NOT NULL constraints require
that computed columns be persisted.
here is a working script (i changed the table name):
CREATE TABLE dbo.[TBL_TXN_USER]
(
[id] int NOT NULL IDENTITY(1,1),
[user_id] as id + 0 persisted not null primary key,
[username] varchar(150) NOT NULL,
[fullname] varchar(150) NOT NUll,
[pwd] varchar(50) NOT NUll,
[email] varchar(150) NOT NULL,
[mobile] varchar(150) NOT NULL,
[designation] varchar(150) NOT NULL,
[deleted] int NULL,
[created_date] datetime NULL,
[creator_user_id] int NULL,
[changed_date] datetime NULL,
[changer_user_id] int NULL,
[add_content] int NULL,
[edit_content] int NULL,
[delete_content] int NULL,
[manage_user] int NULL,
[view_log] int NULL,
);
GO
i have a couple of comments about that question .
- a calculated field with a fixed formula with static values as primary key instead of the id itself is a waste of resources: one of the 2 fields should not be there
- a field with the name of a system function (user_id) is something i would avoid at all costs.
- the question looks like an attempt to put in place a solution (the calculated field as id) for an hidden issue.
Sorry for my misunderstanding, So you want to add auto increments two column in one table. Actually that is not accept at SQL-server so I am going to give you another option below
CREATE TRIGGER [dbo].[insert_triger] ON [dbo].[TBL_TXN_USER]
FOR INSERT
AS
update TBL_TXN_USER set [user_id] = id
where id = (
select MAX(id)
from TBL_TXN_USER
)
column aliases work with select statement not create table, also for [user_id] you didn't provide any data type.
Use the following to create your table :
CREATE TABLE [dbo.TBL_TXN_USER](
[id] int NOT NULL IDENTITY(1,1),
[user_id] int PRIMARY KEY NOT NULL ,
....rest of code
To update [user_id] consider using a trigger.

Retrieve data using select query with null Foreign key value

I have a 2 tables with name Vendor and VendorType . Structure are given
CREATE TABLE XCodesSCMERP.dbo.Vendor (
VendorID INT IDENTITY,
VendorTypeID INT NULL,
VendorName VARCHAR(200) NULL,
VendorCompany VARCHAR(200) NULL,
FirstName VARCHAR(100) NULL,
LastName VARCHAR(100) NULL,
Contact VARCHAR(100) NULL,
Phone VARCHAR(100) NULL,
AltContact VARCHAR(100) NULL,
Email VARCHAR(50) NULL,
AddressBilledFrom VARCHAR(50) NULL,
AddressShippedFrom VARCHAR(50) NULL,
VendorNotes VARCHAR(500) NULL,
OpeningBalance VARCHAR(100) NULL,
OpeningDate VARCHAR(100) NULL,
VendorAccountNo VARCHAR(100) NULL,
CONSTRAINT PK_Vendor PRIMARY KEY CLUSTERED (VendorID),
CONSTRAINT FK_Vendor_VendorTypeTable_VendorTypeID FOREIGN KEY (VendorTypeID) REFERENCES dbo.VendorTypeTable (VendorTypeID)
) ON [PRIMARY]
GO
CREATE TABLE XCodesSCMERP.dbo.VendorTypeTable (
VendorTypeID INT IDENTITY,
VendorType VARCHAR(100) NULL,
VendorDesc VARCHAR(MAX) NULL,
CONSTRAINT PK_VendorTypeTable PRIMARY KEY CLUSTERED (VendorTypeID)
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
It is clear from structures of table that VendorTypeID is foreign key in Vendor table. Now when i want to retrieve data from Vendor table including field VendorType from VendorTypeTable than i have to use Inner Join that works fine.
But here is problem . it does' not show that records which don't have any vendorType i.e VendorTypeID is not selected . It is said to be null.
Now here is my question , how i can retrieve those records also that don't have any Vendor Type.
Would be pleasure for me , helping me in my Problem.
Note:
SELECT VendorID,VendorName,FirstName,LastName,VendorCompany,Contact,Phone,AltContact,Email,OpeningBalance,OpeningDate,VendorAccountNo ,VendorNotes FROM Vendor WHERE VendorTypeID='';
This query does not return any record.
If I understand correctly, you just want IS NULL:
SELECT v.*
FROM Vendor v
WHERE VendorTypeID IS NULL;
Modify your query to something like this
SELECT
VendorID,VendorName,FirstName,LastName,VendorCompany,Contact,Phone,AltContact,Email,OpeningBalance,OpeningDate,VendorAccountNo
,VendorNotes FROM Vendor WHERE VendorTypeID IS NULL;
Note: A NULL value is different from a zero value or a field that contains spaces. A field with a NULL value is a blank field