I am using Installshield 2015 version.We have created a install script .msi project.
Under SQL Scripts Tab, we have created new SQL Connection.
I am providing CatalogName "NMC" and selected checkbox(Create catalog if absent).
I am running the below SQL Scripts to run against this Database(NMC).
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Account]') AND type in (N'U'))
BEGIN
PRINT 'Tables have already been created'
END
ELSE
BEGIN
/****** Object: Table [dbo].[Rights] ******/
CREATE TABLE [dbo].[Rights](
[RightId] [int] NOT NULL,
[Name] [nvarchar](50) NOT NULL,
CONSTRAINT [PK_Rights] PRIMARY KEY CLUSTERED
(
[RightId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
I want the Table created on the Database which is mentioned in Catalog(NMC).
Right now the script is executing against Master Database.
How can I execute the script against Catalog mentioned above?I don't want to use command "USE NMC" in SQL Script.
You may change default database MNC for login you use in top part & again change to master at the end...
Exec sp_defaultdb #loginame='login', #defdb='MNC'
Exec sp_defaultdb #loginame='login', #defdb='master'
Related
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.
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
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
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.
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