Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9228415
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T05:19:07+00:00 2026-06-18T05:19:07+00:00

I have a database wich contain 4 tables -battery -cell -owner each battery have

  • 0

I have a database wich contain 4 tables

-battery
-cell
-owner

each battery have multiple cell and one owner.

The cell table will contain a tons of entry.
i wanted a trigger that create view for each new battery that display the battery information and its cell’s information .

I had a postgre sql trigger that worked kinda well (thanks to stackoverflow 🙂 ) :

    BEGIN

EXECUTE $$CREATE OR REPLACE VIEW battery_vue
AS
SELECT * FROM cells WHERE battery_serial = $$ || NEW.battery_serial;

RETURN NEW;

END;

but i’m unable to transpose it to SQL server and i have absolutly no clue on how to do it.

here is a simple diagram of my database :

DB Diagram

So at the end for each new battery i need a view of it with its cells.

Ps : sorry if my english is not fluent :/

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T05:19:08+00:00Added an answer on June 18, 2026 at 5:19 am

    Hopefully this points you in the right direction…

       /*Create a test table*/
    IF OBJECT_ID('TestBattery') IS NOT NULL
    BEGIN
        DROP TABLE dbo.[TestBattery]
    END
    GO
    
    CREATE TABLE dbo.[TestBattery]
    (
    id          INT
    ,owner_id    INT
    ,data_id     INT
    )
    GO
    
    /*Create a test trigger*/
    IF OBJECT_ID('trg_TestBattery_CreateView') IS NOT NULL
    BEGIN
        DROP Trigger dbo.trg_TestBattery_CreateView
    END
    GO
    
    
    CREATE TRIGGER [dbo].[trg_TestBattery_CreateView]
    ON [dbo].[TestBattery] AFTER INSERT
    AS
    SET NOCOUNT ON 
    
    DECLARE @BatteryID      INT
            ,@View          VARCHAR(200)
            ,@SQLCreateView VARCHAR(MAX)
    
    SELECT @BatteryID = ID FROM INSERTED
    
    /*Dynamically generate new view name based on BatteryID that was inserted*/
    SELECT @View = 'TestBattery_'+CAST(@BatteryID AS VARCHAR)
    
    /*Generate SQL to create the view*/
    SELECT @SQLCreateView = 'CREATE VIEW '+@View+' AS SELECT [id],[owner_id],[data_id] FROM dbo.[TestBattery] WHERE ID = '+CAST(@BatteryID AS VARCHAR)
    
    /*We create the view only if it doesn't exist*/
    IF OBJECT_ID(@View) IS NULL
    BEGIN
    
    EXEC(@SQLCreateView)
    
    END
    GO
    
    /*Testing your trigger*/
    INSERT INTO TestBattery
    (
    id
    ,owner_id
    ,data_id
    )
    SELECT
    '1'
    ,'2'
    ,'3'
    
    SELECT * FROM TestBattery
    SELECT * FROM TestBattery_1
    
    /*Take out the trash*/
    DROP TABLE TestBattery
    DROP VIEW TestBattery_1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have database with multiple tables in Microsoft SQL Server with schema in tables
I have database, and in one the tables I need change the values of
i have database table like this +-------+--------------+----------+ | id | ip | date |
Currently I have database with the following associations: One Client to Many Intakes One
that my problem: I have database table like that: id (AI) market_id 1 6
I have quite a few situations where I have database structures similar to: TABLE
Ok the title maybe a little confusing. I have a database with the table
I have a simple php/html form wich inserts data to my mysql database. I
I Have a Datagridview bound to a datatable wich hold data from a database
Hello guys i have an website online wich access the database. The database access

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.