How to populate one column based on another column
JLueders
Posts: 10 New member
I have a Jobs table I am trying to populate that has the columns Id, FacilityId, and ContactId (among others). There is a Contacts table that has Id, Name, and FacilityID. When generating data for the Jobs table, I want to make sure the ContactId is valid for the FacilityId based on the Contacts table. In other words, I want the ContactId and FacilityId combination to exist in the Contacts table. Is this possible?
Tagged:
Answers
Hi JLueders,
Thank you for reaching out regarding SQL Data Generator. For aligning the
ContactId
andFacilityId
combination in theJobs
table with valid entries in theContacts
table, I recommend utilizing the automatic "Foreign Key generator". This option seamlessly maps and references the primary key relationships in your database, ensuring data consistency.For a more information, please refer to our documentation: Using Generators (SQL Data Generator Documentation)
Warm regards,
Hassan
Product Support Engineer
Hi JLueders,
Thank you for providing clarity on the issue. To ensure that theFacilityId
in theJobs
table matches the one associated with the chosenContactId
in theContacts
table, we need a custom solution. For theFacilityId
column in theJobs
table, instead of using the default foreign key generator (which just ensures it's a valid facility), use the "SQL Statement generator." The statement will fetch the correspondingFacilityId
for the generatedContactId
. The SQL statement would be something like:I also tried the following:
SELECT Contacts.FacilityId FROM Contacts
join Jobs on Contacts.Id = Jobs.ContactId
This gives the message "Not enough rows for column 'FacilityId' in the preview.
Hi JLueders,
Thank you for reaching out regarding SQL Data Generator. For aligning the
ContactId
andFacilityId
combination in theJobs
table with valid entries in theContacts
table, I recommend utilizing the automatic "Foreign Key generator". This option seamlessly maps and references the primary key relationships in your database, ensuring data consistency.For a more information, please refer to our documentation: Using Generators (SQL Data Generator Documentation)
Warm regards,
Hassan
Product Support Engineer
Hi Leuders,
I sincerely apologize for the delay in addressing your concern. Regarding the error message "Not enough rows for column 'FacilityId'", Im not seeing this on my end. Could you kindly share the DDL for your tables? This will enable me to replicate and further investigate the matter.
All the best,
Hassan
Product Support Engineer
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Facilities](
[Id] [int] IDENTITY(1,1) NOT NULL,
[Name] [nvarchar](1000) NOT NULL,
[StreetAddress] [nvarchar](1000) NOT NULL,
[City] [nvarchar](1000) NOT NULL,
[ZipCode] [nvarchar](20) NOT NULL,
[PhoneNumber] [nvarchar](100) NOT NULL,
[MailingAddress] [nvarchar](1000) NOT NULL,
[MailingCity] [nvarchar](1000) NOT NULL,
[MailingZipCode] [nvarchar](20) NOT NULL,
[StateId] [int] NOT NULL,
CONSTRAINT [PK_Facilities] PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
CREATE TABLE [dbo].[Contacts](
[Id] [int] IDENTITY(1,1) NOT NULL,
[FacilityId] [int] NULL,
[FirstName] [nvarchar](200) NOT NULL,
[LastName] [nvarchar](200) NOT NULL,
[Email] [nvarchar](200) NOT NULL,
[WorkPhone] [nvarchar](100) NOT NULL,
[CellPhone] [nvarchar](100) NOT NULL,
[Extension] [nvarchar](100) NOT NULL,
CONSTRAINT [PK_Contacts] PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[Contacts] WITH CHECK ADD CONSTRAINT [FK_Contacts_Facilities_FacilityId] FOREIGN KEY([FacilityId])
REFERENCES [dbo].[Facilities] ([Id])
GO
ALTER TABLE [dbo].[Contacts] CHECK CONSTRAINT [FK_Contacts_Facilities_FacilityId]
GO
CREATE TABLE [dbo].[Jobs](
[Id] [int] IDENTITY(1,1) NOT NULL,
[FacilityId] [int] NULL,
[DisciplineSpecialtyId] [int] NOT NULL,
[JobTypeId] [int] NOT NULL,
[ContactId] [int] NOT NULL,
[Active] [bit] NOT NULL,
CONSTRAINT [PK_Jobs] PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
ALTER TABLE [dbo].[Jobs] WITH CHECK ADD CONSTRAINT [FK_Jobs_Contacts_ContactId] FOREIGN KEY([ContactId])
REFERENCES [dbo].[Contacts] ([Id])
GO
ALTER TABLE [dbo].[Jobs] CHECK CONSTRAINT [FK_Jobs_Contacts_ContactId]
GO
ALTER TABLE [dbo].[Jobs] WITH CHECK ADD CONSTRAINT [FK_Jobs_Facilities_FacilityId] FOREIGN KEY([FacilityId])
REFERENCES [dbo].[Facilities] ([Id])
GO
ALTER TABLE [dbo].[Jobs] CHECK CONSTRAINT [FK_Jobs_Facilities_FacilityId]
GO
Sorry I didn't realize this sooner. The DDL I sent is correct.
Hey JLueders,
Thank you for your patience and providing the DDL for your tables. I'll need a bit more time to thoroughly test and ensure we have a script that works seamlessly for your requirements. Please bear with me; I should have a solution ready for you by Thursday.
Warm regards,
Hassan
Product Support Engineer
Hi JLueders,
Thank you for your patience and for providing detailed information regarding your table structures.
Regarding the challenge you're facing with the
FacilityId
andContactId
combination When a column is set with theIDENTITY
property, it's controlled by the server to auto-generate values. This ensures uniqueness and is typically used for primary key columns.If you're trying to generate or insert specific values into an identity column, such as
Id
in yourJobs
table, In order to generate into that column, you need to remove that IDENTITY constraint. After that is removed you can move forward without using SQL Statements and just set the relation to user defined foreign key for the columnBest,
Hassan
Product Support Engineer
Hi JLueders,
Apologies for the extended delay. During the process of finding a solution, a few bugs were encountered that might have been causing the issue.
To begin, it’s important to note that using SQL Statement options won’t be necessary. Instead, some changes need to be made in the job table. Specifically, in the default foreign key (Automatic) Column Generation settings, the population method should be set to “All Key Values Unique”. Additionally, the ‘allow nulls’ box for both of the foreign keys, ‘contactid’ and ‘facilityid’, should be unchecked.
This should help in resolving the issue.
Best regards,
Hassan
Product Support Engineer
I was able to make it work using a SQL Statement.
One other question: When using a SQL Statement for a column, is there a way to have it use the same connection as the project?
I would like to clarify your requirement. Are you suggesting that instead of manually choosing a database for each SQL statement, we set a default database aligned with the current project?
Best,
Hassan
That is correct. This way, if I need to generate data for a different database, I only have to change the default database for the project and not go into every column that uses a SQL Statement.
Jeff
I regret to inform you that the option you're inquiring about is not available at this time. Nonetheless, I recognize the value it could add to streamlining the project. Rest assured, I will raise this matter with our development team for consideration.
Thank you,
Hassan
Product Support Engineer