How to create database view for 2 tables

Tharun_M
Tera Contributor

Hello everyone,

 

I'm new to database view.

 

I have two tables

 

Table1 extends task

Table1 has 2 fields 

field1 - String (Example. Phone)

field2 - Reference to table2 (Example. Samsung)

 

Table2 does not extend any table

Table2 has 2 fields 

field1 - String(this is the reference record used in table1) (Example. Samsung)

field2 - String(Example. note 15)

 

Is there any way where I could see field1, field2 of table1 and field1 field2 of table2. (Phone Samsung Samsung Note 15) using database view?

1 ACCEPTED SOLUTION

Danish Bhairag2
Tera Sage
Tera Sage

@Tharun_M ,

 

In ur case when u create database view based upon prefix wht u provide it should look something like below

 

You should write this in where clause of table 2

 

Lets say prefix u have kept is table1 & table2

 

Then ur where clause would look like

table1.field2=table2.sys_id

 

Thanks,

Danish

View solution in original post

8 REPLIES 8

Anand Kumar P
Giga Patron
Giga Patron

Hi @Tharun_M ,

You can take below link as reference on incident and problem table
https://www.servicenow.com/community/developer-forum/database-view-select-data-from-2-tables/m-p/135...  

 

Mark it as helpful and solution proposed if it serves your purpose.
Thanks,
Anand

Sonam_Tiwari
Kilo Sage

Hi,

Create the database view with view tables - Table1 and Table 2 and

add a left join with a where clause as - WHERE Table1<var prefix>.field2 = Table2<var prefix>.field1

 

Try once

 

 

 

 

 

Consider indicating the response as helpful and marking it as correct if it meets your needs.

SANDEEP28
Mega Sage

@Tharun_M Below blog will definitely help you to understand the database view. Also see the video linked in it.

 

https://www.servicenow.com/community/developer-blog/what-is-servicenow-database-view-create-servicen...

 

For your case, it is possible to create database view and get all fields of table1 & table2 in single view. You can refer below OOB database view for reference.

 

SANDEEP28_0-1707804007104.png

 

 

If I could help you with your Query then, please hit the Thumb Icon and mark as Correct !!

 

AndersBGS
Tera Patron
Tera Patron

Hi @Tharun_M 

 

According to your description, you state that table1 field2 and table2 field 1 is both string fields but also reference fields - what is correct? If it is reference fields, then you can just dot-walk without the need of a database view. Note that your table1.field2 and table2.field1 is Samsung. I would assume that there are several samsung models, so not the best option to do a database view upon. To create a Database view, you should have a 1-1 or a 1-m relationship between the two tables. 

 

If my answer has helped with your question, please mark my answer as accepted solution and give a thumb up.

 

Best regards

Anders 

If my answer has helped with your question, please mark my answer as the accepted solution and give a thumbs up.

Best regards
Anders

Rising star 2024
MVP 2025
linkedIn: https://www.linkedin.com/in/andersskovbjerg/