Powershell SqlServer Modules Write-SqlTableData Computed Column

Van Thieu 0 Reputation points
2023-03-16T15:22:34.8866667+00:00

Let's assume a table has a computed column, using the SqlServer PowerShell module command Write-SqlTableData, how does one manage to insert data onto the table if a computed column exists? Thanks!

I'm running into and error:
The column "A" cannot be modified because it is either a computed column or is the result of a UNION operator.

Windows for business Windows Server User experience PowerShell
SQL Server Other
{count} votes

2 answers

Sort by: Most helpful
  1. Sudipta Chakraborty 1,116 Reputation points
    2023-03-16T19:40:34.9233333+00:00

    @Van Thieu :

    You will need to specify the columns you want to insert values into. Please find the below mentioned sample SQL script where InventoryValue is the computed field.

    Reference: https://learn.microsoft.com/en-us/sql/relational-databases/tables/specify-computed-columns-in-a-table?view=sql-server-ver16

    CREATE TABLE dbo.Products
       (
          ProductID int IDENTITY (1,1) NOT NULL
          , QtyAvailable smallint     
          , InventoryValue AS QtyAvailable * UnitPrice
    	  , UnitPrice money
        );
    
    INSERT INTO dbo.Products (QtyAvailable, UnitPrice) VALUES (25, 2.00), (10, 1.5);
    
    select * from dbo.Products
    
    0 comments No comments

  2. Olaf Helper 47,436 Reputation points
    2023-03-17T07:14:22.4933333+00:00

    how does one manage to insert data onto the table if a computed column exists?

    By not inserting into/addressing the computed column; by the error message you try to do so; that's your failure here.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.