Generating script from existing table having too much detail - sql-server

When i use SQL Server Management Studio GUI to generate script of a table of an existing database, it produces the following:
USE [BikeStores]
GO
/****** Object: Table [production].[brands2] Script Date: 9/20/2019 3:44:27 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [production].[brands2](
[brand_id] [int] IDENTITY(1,1) NOT NULL,
[brand_name] [varchar](255) NOT NULL,
PRIMARY KEY CLUSTERED
(
[brand_id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
But i want the simple table script as follows
CREATE TABLE production.brands2 (
brand_id INT IDENTITY (1, 1) PRIMARY KEY,
brand_name VARCHAR (255) NOT NULL
);

You will not be able to create the desired output in SSMS. However, you can do some slight changes in SSMS of what to output when you script your table. For example you can choose not to
Script Indexes, Triggers and Foreign keys. Etc..
Go to Tools -> Options -> Sql Server Object Explorer -> Scripting

Related

Why RowVersion datatype is missing inside SSMS

We have SQL server 2017, and we want to create a new field inside existing database
the field data type is RowVersion
but using the SQL Management Studio I can not define a field with RowVersion data type
we can use Timestamps
but per my knowledge TimeStamp are now deprecated in favor of RowVersion
Any advice on this?
Here is the DataType list which does not contain rowversion:
EDIT
Now i wrote the following script to create a new table with rowversion column type:-
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[test2](
[id] [int] NOT NULL,
[rowversion] [rowversion] NOT NULL,
CONSTRAINT [PK_test2] PRIMARY KEY CLUSTERED
(
[id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
but when i open the table inside the SQL management studio GUI >> the type for the new column inside the new table will be timestamp instead of rowversion + if i generate a Create to script for the new table i will get this:-
USE [test]
GO
/****** Object: Table [dbo].[test2] Script Date: 26/08/2021 19:02:56 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[test2](
[id] [int] NOT NULL,
[rowversion] [timestamp] NOT NULL,
CONSTRAINT [PK_test2] PRIMARY KEY CLUSTERED
(
[id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
so seems rowversion can be used to create the table but it will be converted to timestamp... the issue is that Microsoft say that timestamp is deprecated and that we should use rowversion instead... totally confusing!!
This was an error in the microsoft documentation. I submitted a pull request to have that corrected, and that PR has been accepted.
The problem, and the thing confusing you here, was that the documentation claimed that "timestamp" was a synonym for "rowversion". But in fact the opposite is true. "Rowversion" is the synonym, "timestamp" is the base type name.
Both names actually refer to the same thing under the covers, but different tools have differing levels of support for synonyms. The graphical designers are old and have not been updated in a very, very long time.

Issue with relationship in SQL Server and Vb.net

I have a table of "CASH PAYMENT". I created two tables in SQL Server 2008 and did drag and drop in vb.net 2010.
Now when I save it with default update code, I get different errors.
I do not have any idea of relationships. I searched the web about it but did not find anything related to it. Also, what code can I use to update my relationship entry for vb.net using SQL Server?
I tried nothing - I only drag and drop the SQL Server database in vb.net and with default update code I got error:
The INSERT statement conflicted with the FOREIGN KEY constraint "FK_cash-entries_cash-entries". The conflict occurred in database "dbo_main", table "dbo.cash_payn_details", column 'autonumber'
Code is shown below. This is the default code when drag drop SQL Server in vb.net
Me.Validate()
Me.Cash_payn_detailsBindingSource.EndEdit()
Me.TableAdapterManager.UpdateAll(Me.Dbo_mainDataSet)
Me.Cash_entriesBindingSource.EndEdit()
Me.Cash_entriesTableAdapter.Update(Me.Dbo_mainDataSet)
I want to save two tables with one reference number. I am a beginner in vb.net and SQL Server relationships.
Edit
Table structure:
USE [dbo_main]
GO
/****** Object: Table [dbo].[cash_payn_details] Script Date: 01/04/2019 17:44:25 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[cash_payn_details](
[autonumber] [nchar](10) NOT NULL,
[account] [nchar](10) NULL,
[amount] [nchar](10) NULL,
[date] [nchar](10) NULL,
CONSTRAINT [PK_cash_payn_details] PRIMARY KEY CLUSTERED
(
[autonumber] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
OTHER IS
USE [dbo_main]
GO
/****** Object: Table [dbo].[cash-entries] Script Date: 01/04/2019 17:45:08 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[cash-entries](
[des_autonumber] [nchar](10) NOT NULL,
[system_ID] [nchar](10) NULL,
[entries] [nvarchar](50) NULL,
CONSTRAINT [PK_cash-entries] PRIMARY KEY CLUSTERED
(
[des_autonumber] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[cash-entries] WITH CHECK ADD CONSTRAINT [FK_cash-entries_cash-entries] FOREIGN KEY([system_ID])
REFERENCES [dbo].[cash_payn_details] ([autonumber])
GO
ALTER TABLE [dbo].[cash-entries] CHECK CONSTRAINT [FK_cash-entries_cash-entries]
GO
INSERT INTO [dbo_main].[dbo].[cash-entries]
([des_autonumber]
,[system_ID]
,[entries])
VALUES
(<des_autonumber, nchar(10),>
,<system_ID, nchar(10),>
,<entries, nvarchar(50),>)
GO
other is
INSERT INTO [dbo_main].[dbo].[cash_payn_details]
([autonumber]
,[account]
,[amount]
,[date])
VALUES
(<autonumber, nchar(10),>
,<account, nchar(10),>
,<amount, nchar(10),>
,<date, nchar(10),>)
GO

Deleting records using DeleteAllOnSubmit not working with FK contstraints

Im trying to use Linqpad so as to use LINQ instead of TSQL when trying to remove some records from a table that is part of a "dependency tree" that spans probably 10-12 tables. So I created some tables in a test db and im experimenting with the DeleteAllOnSubmit and SubmitChanges methods.
I get an FK error. Yet, Im deleting the child objects first. How do I delete when there is FK relations (without using cascading on the FK's)?
The DELETE statement conflicted with the REFERENCE constraint
"FK_OrderItem_Order". The conflict occurred in database "Test", table
"dbo.OrderItem", c...
Tables:
USE [Test]
GO
/****** Object: Table [dbo].[Order] Script Date: 6/9/2016 2:50:18 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[Order](
[Id] [int] IDENTITY(1,1) NOT NULL,
[OrderNum] [varchar](20) NOT NULL,
[Description] [varchar](100) NULL,
CONSTRAINT [PK_Order] PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/****** Object: Table [dbo].[OrderItem] Script Date: 6/9/2016 2:50:18 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[OrderItem](
[Id] [int] IDENTITY(1,1) NOT NULL,
[OrderId] [int] NOT NULL,
[OrderItemName] [varchar](100) NOT NULL,
[Qty] [int] NOT NULL,
CONSTRAINT [PK_OrderItem] PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
ALTER TABLE [dbo].[OrderItem] WITH CHECK ADD CONSTRAINT [FK_OrderItem_Order] FOREIGN KEY([OrderId])
REFERENCES [dbo].[Order] ([Id])
GO
ALTER TABLE [dbo].[OrderItem] CHECK CONSTRAINT [FK_OrderItem_Order]
GO
Here is my LINQ statement
List<Order> olist = (from a in Orders where a.Id == 2 select a).ToList();
List<int> olistidlist = olist.Select (o => o.Id).ToList();
List<OrderItem> oilist = (from a in OrderItems where olistidlist.Contains(a.Id) select a).ToList();
//see results before
olist.Dump();
OrderItems.DeleteAllOnSubmit(oilist);
Orders.DeleteAllOnSubmit(olist);
//save changes
SubmitChanges();
//see results after
olist.Dump();
You are deleting OrderItems of which the OrderItem.Id is equal to values in olistidlist (only 2, presently). Of course, this must be OrderItem.OrderId.
By the way, an easier way to delete parent and child items generally is:
foreach (var order in Orders.Where(...))
{
OrderItems.DeleteAllOnSubmit(order.OrderItems);
Orders.DeleteOnSubmit(order);
}
So you don't have to move around with Id values.

Generating a Database from SQL script in Visual Studio

I was recently given a project using ASP.NET, C#, and an SQL database. I am somewhat familiar with ASP.NET and C#, however I have never used the Microsoft database software in visual studio. I simply need to generate a database from an SQL script given to me. I was able to get the database mostly generated, however some of the code (The part adding items to one of the tables) did not run on execution. I have tried adding other scripts and rerunning the first one, I cannot get anything to work. I know the script is fine, I just cant get it to run correctly. Thanks for the help. Here's the script
/*These first few lines don't run */
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[Product](
[ID] [int] IDENTITY(1,1) NOT NULL,
[Name] [varchar](128) NOT NULL,
CONSTRAINT [PK_Product] PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
CREATE TABLE [dbo].[ProductNote](
[ID] [int] IDENTITY(1,1) NOT NULL,
[ProductID] [int] NOT NULL,
[NoteText] [nvarchar](1000) NOT NULL,
[CreateDate] [datetime] NOT NULL DEFAULT (GETDATE()),
[Archived] [BIT] NOT NULL DEFAULT (0)
CONSTRAINT [PK_ProductNote] PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[ProductNote] WITH CHECK ADD CONSTRAINT [FK_ProductNote_Product] FOREIGN KEY([ProductID])
REFERENCES [Product] ([ID])
GO
ALTER TABLE [dbo].[ProductNote] CHECK CONSTRAINT [FK_ProductNote_Product]
GO
/*This part does not run either */
DECLARE #n INT = 1
WHILE (#n <=20)
BEGIN
INSERT INTO dbo.Product (Name) SELECT 'Product ' + CONVERT(VARCHAR(10),#n)
SET #n = #n + 1
END
SET ANSI_PADDING OFF
GO
In Visual Studio
Go to View->Solution Explorer
Then Connect your sql Data Base
following links might be helpfull ro connect your db in visual studio
1.http://support.webecs.com/kb/a204/how-do-i-connect-to-sql-server-using-visual-studio-_net.aspx
2.https://support.zen.co.uk/kb/Knowledgebase/SQL-Server-Connecting-to-your-database-through-code-or-Visual-StudioNET
Expand your DB then Right Click on Stored Procedure -> Add new stored procedure
Replace all text with your script->Select All->Right Click ->Run Selection
That's it.

Identity Problems During Database Creation

Using SQL Server Management Studio, my issue stems from a database creation script. The script is written to create a database, many of whose tables have an identity column:
CREATE TABLE Workshop
(
WorkshopID int IDENTITY,
WorkshopName varchar(40) NOT NULL,
Description varchar(800),
CONSTRAINT PK_Workshop PRIMARY KEY (WorkshopID)
);
My issue is that even with the script plainly creating a column as an identity column, after the script runs none of the columns that should be identity columns actually have that column set to be identity.
To clarify: Running the above code will create that table as specified except WorkshopID will not be an identity column.
What needs to change so that the script will work as written?
FYI, if you generate script for this using SQL Management Studio's designer, this is the resulting script:
/* To prevent any potential data loss issues, you should review this script in detail before running it outside the context of the database designer.*/
BEGIN TRANSACTION
SET QUOTED_IDENTIFIER ON
SET ARITHABORT ON
SET NUMERIC_ROUNDABORT OFF
SET CONCAT_NULL_YIELDS_NULL ON
SET ANSI_NULLS ON
SET ANSI_PADDING ON
SET ANSI_WARNINGS ON
COMMIT
BEGIN TRANSACTION
GO
CREATE TABLE dbo.Table_1
(
WorkshopID int NOT NULL IDENTITY (1, 1),
WorkshopName varchar(40) NOT NULL,
Description varchar(800) NULL
) ON [PRIMARY]
GO
ALTER TABLE dbo.Table_1 ADD CONSTRAINT
PK_Table_1 PRIMARY KEY CLUSTERED
(
WorkshopID
) WITH( STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
ALTER TABLE dbo.Table_1 SET (LOCK_ESCALATION = TABLE)
GO
COMMIT
If you create the table and then script it using the Create To... menu option you get a completely different script:
USE [MyDatabase]
GO
/****** Object: Table [dbo].[Workshop] Script Date: 11/27/2012 14:05:33 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[Workshop](
[WorkshopID] [int] IDENTITY(1,1) NOT NULL,
[WorkshopName] [varchar](40) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[Description] [varchar](800) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
CONSTRAINT [PK_Workshop] PRIMARY KEY CLUSTERED
(
[WorkshopID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO

Resources