I' trying to generate the scripts for ma DB in Sql Server 2008.. and i'm able to do that, the scripts generated is :
USE [Cab_Booking]
GO
/****** Object: Table [dbo].[User] Script Date: 05/19/2013 10:33:05 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[User]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[User](
[U_Id] [int] IDENTITY(1,1) NOT NULL,
[UserName] [nvarchar](50) NOT NULL,
[Password] [nvarchar](50) NOT NULL,
add column new int not null,
CONSTRAINT [PK_User] PRIMARY KEY CLUSTERED
(
[U_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]
END
GO
What should i do if i need to add a new column in my table through scripts...
I know this sounds easy...But, i dont know what am i missing...
thanks..
Right click on the table name on SQL Server Management Studio and select "Design". Then add a column using designer, but don't save. Right click anywhere on table designer and select "Generate Change Script". Now you have the script required to add new column to table. This method also works for removing columns, changing data types, etc.
Related
I'm trying to translate a LINQ to SQL from C# to F#. This is what I've come up with:
let get (roles : List<string>) =
query {
for t in db.Tabs do
join r in db.Regions on (t.Id = r.FkTab)
join l in db.Links on (r.Id = l.FkRegion)
where (roles.Contains(l.Role) || roles.Contains("Administrator"))
groupValBy (t,r,l) (t.Id, t.Label) into tt
select {
Label = snd tt.Key;
Regions = query {
for xr in db.Regions do
join xl in db.Links on (xr.Id = xl.FkRegion)
where (xr.FkTab.Equals (fst tt.Key) && (roles.Contains(xl.Role) || roles.Contains("Administrator")))
groupValBy (xr, xl) (xr.Id, xr.Label) into rr
select {
Label = snd rr.Key;
Links = query {
for yl in db.Links do
where (yl.FkRegion.Equals (fst rr.Key) && (roles.Contains(yl.Role) || roles.Contains("Administrator")))
select {
Label = yl.Label;
Url = yl.Url;
Role = yl.Role
}
} |> Seq.toList
}
} |> Seq.toList
}
} |> Seq.toList
Instead of classes I've used records, here they are:
type Link = {Url : string; Role : string; Label : string}
type Region = {Label : string; Links : List<Link>}
type Tab = {Label : string; Regions : List<Region> }
Unfortunately I'm getting an exception at runtime:
A first chance exception of type 'System.NotSupportedException' occurred in System.Data.Linq.dll
The message is: System.NotSupportedException: Query operator 'AsQueryable' not supported.
I'm very green to F#, what am I missing? Perhaps I cannot use records in LINQ to SQL? I cannot nest in that manner?
Thanks
EDIT:
db is the database context:
let db = SqlConnection.GetDataContext()
If you wish to try this at home, here is the script to create the three tables needed:
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[Links](
[id] [int] IDENTITY(1,1) NOT NULL,
[fkRegion] [int] NOT NULL,
[label] [varchar](50) NOT NULL,
[role] [varchar](50) NOT NULL,
[url] [varchar](100) NOT NULL,
CONSTRAINT [PK_Links] 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].[Regions] Script Date: 22/08/2015 12:06:49 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[Regions](
[id] [int] IDENTITY(1,1) NOT NULL,
[fkTab] [int] NOT NULL,
[label] [varchar](50) NOT NULL,
CONSTRAINT [PK_Regions] 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].[Tabs] Script Date: 22/08/2015 12:06:49 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[Tabs](
[id] [int] IDENTITY(1,1) NOT NULL,
[label] [varchar](50) NOT NULL,
CONSTRAINT [PK_Tabs] 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].[Links] WITH CHECK ADD CONSTRAINT [FK_Links_Regions] FOREIGN KEY([fkRegion])
REFERENCES [dbo].[Regions] ([id])
GO
ALTER TABLE [dbo].[Links] CHECK CONSTRAINT [FK_Links_Regions]
GO
ALTER TABLE [dbo].[Regions] WITH CHECK ADD CONSTRAINT [FK_Regions_Tabs] FOREIGN KEY([fkTab])
REFERENCES [dbo].[Tabs] ([id])
GO
ALTER TABLE [dbo].[Regions] CHECK CONSTRAINT [FK_Regions_Tabs]
GO
EDIT2:
The issue seems to be with the result of the query. I tried changing the record type to IEnumerable and removing the Seq.toList. However, whenever I try any operation on the enumerable, the same exception pops up, e.g. Model.Count() (where Model is an IEnumerable). Help?
I am using SQL server 2008, Want to import my db to Azure. I have (*.bak) file. Is their any work around to restore my db to Azure without changing my db structure.
I tried SQLAzureMW but it is giving me this error
'Filegroup reference and partitioning scheme' is not supported in this version of SQL Server.
I searched Filegroup keyword in scripts but it isn't there.
I also tried Azure SilverLight Managment Tool but it giving me same error.
While running this script i am getting the above error.
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [Mohsin].[Supplier](
[SuppID] [int] NOT NULL,
[Name] [varchar](50) NULL,
[street] [varchar](30) NULL,
[City] [varchar](20) NULL,
[State] [varchar](20) NULL,
[County] [varchar](30) NULL,
[PostalCode] [varchar](25) NULL,
[Phone] [varchar](17) NULL,
[Fax] [varchar](17) NULL,
[Active] [bit] NULL,
CONSTRAINT [PK_Supplier] PRIMARY KEY CLUSTERED
(
[SuppID] 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
Make sure there isn't a reference for an Index or object indicating that it should go on the Primary filegroup. Search for ON [PRIMARY] or ON PRIMARY, or just the word PRIMARY.
The SQL Azure Migration Wizard is usually pretty good about pointing out exactly what it doesn't like.
I need to import the data from the multiple distributed database ( around 70 ) to the single source table .So how is it possible through SSIS 2008
Assuming that you can run the same query against each of the 70 source servers, you can use a ForEach Loop with a single Data Flow Task. The source connection manager's ConnectionString should be an expression using the loop variables.
Here's an example reading the INFORMATION_SCHEMA.COLUMNS view from multiple DBs. I created the following tables on my local instance:
<!-- language: lang-sql -->
CREATE TABLE [MultiDbDemo].[SourceConnections](
[DatabaseKey] [int] IDENTITY(1,1) NOT NULL,
[ServerName] [varchar](50) NOT NULL,
[DatabaseName] [varchar](50) NOT NULL,
CONSTRAINT [PK_SourceConnections] PRIMARY KEY CLUSTERED
(
[DatabaseKey] 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 [MultiDbDemo].[SourceColumns](
[ColumnKey] [int] IDENTITY(1,1) NOT NULL,
[ServerName] [varchar](50) NOT NULL,
[DatabaseName] [varchar](50) NOT NULL,
[SchemaName] [varchar](50) NOT NULL,
[TableName] [varchar](50) NOT NULL,
[ColumnName] [varchar](50) NOT NULL,
CONSTRAINT [PK_SourceColumns] PRIMARY KEY CLUSTERED
(
[ColumnKey] 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
This is the control flow for the SSIS package:
The Source_AdoDotNet connection manager's ConnectionString property is set to the following expression:
SQL_GetSourceList's SQLStatement property is SELECT ServerName, DatabaseName FROM MultiDbDemo.SourceConnections, and the ResultSet is mapped to the User::SourceList variable.
The ForEach Loop task is configured thusly:
Note that the ADO object source variable is set to the User::SourceList variable populated in the SQL_GetSourceList task.
And the data flow looks like this:
ADO_SRC_SourceInfo is configured thusly:
The next effect of all this is that, for each database listed in the SourceConnections table, we execute the query SELECT LEFT(TABLE_SCHEMA, 50) AS SchemaName, LEFT(TABLE_NAME, 50) AS TableName, LEFT(COLUMN_NAME, 50) AS ColumnName FROM INFORMATION_SCHEMA.COLUMNS and save the results in the SourceColumns table.
You will still need 70 destination components. Simply specify the same table in all of them.
I have a MSSQL queries file(.sql), now I need to convert it to MYSQL queries.
Please help me. The script like this:
CREATE TABLE [dbo].[Artist](
[ArtistId] [int] IDENTITY(1,1) NOT NULL,
[Name] [nvarchar](120) NULL,
PRIMARY KEY CLUSTERED
(
[ArtistId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
If you want to convert the DDL by hand, then you can do this by building up rules on a case by case basis, e.g. as follows:
[] need to be replaced with backticks
IDENTITY(1,1) can be replaced with AUTO_INCREMENT
Most of the ANSI options and Device settings
can be ignored (these seem to be present only because the table has
been rescripted)
w.r.t. dbo, MySQL doesn't implement schemas in the same way as SQL Server - you will either need to separate schemas into databases, or drop the schema, or mangle the schema name into the tablename (e.g. as a Prefix)
This will leave you with something like the following:
CREATE TABLE `Artist`(
`ArtistId` int NOT NULL AUTO_INCREMENT,
`Name` nvarchar(120) NULL,
PRIMARY KEY CLUSTERED
(
`ArtistId` ASC
)
);
Fiddle here
However, it is usually much easier to do this migration with a migration tool - search for the section on How to Transition from SQL Server to MySQL
Software used:
Visual studio 2008 professional with services pack 1
Sql Server 2005 Standard Edition (9.00.4266.00)
Windows XP SP3
I have these 3 tables:
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[Table_2](
[table2id] [int] IDENTITY(1,1) NOT NULL,
[table2filler] [varchar](max) NULL,
CONSTRAINT [PK_Table_2] PRIMARY KEY CLUSTERED
(
[table2id] 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
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Table_1](
[table1id] [int] IDENTITY(1,1) NOT NULL,
[table1guid] [uniqueidentifier] NOT NULL,
CONSTRAINT [PK_Table_1] PRIMARY KEY CLUSTERED
(
[table1id] 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 UNIQUE NONCLUSTERED INDEX [IX_Table_1] ON [dbo].[Table_1]
(
[table1guid] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[Table_3](
[tableguid] [uniqueidentifier] NOT NULL,
[table2id] [int] NOT NULL,
[table3filler] [varchar](max) NULL,
CONSTRAINT [PK_Table_3] PRIMARY KEY CLUSTERED
(
[tableguid] ASC,
[table2id] 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].[Table_3] WITH CHECK ADD CONSTRAINT [FK_Table_3_Table_1] FOREIGN KEY([tableguid])
REFERENCES [dbo].[Table_1] ([table1guid])
ON DELETE CASCADE
GO
ALTER TABLE [dbo].[Table_3] CHECK CONSTRAINT [FK_Table_3_Table_1]
GO
ALTER TABLE [dbo].[Table_3] WITH CHECK ADD CONSTRAINT [FK_Table_3_Table_2] FOREIGN KEY([table2id])
REFERENCES [dbo].[Table_2] ([table2id])
ON DELETE CASCADE
GO
ALTER TABLE [dbo].[Table_3] CHECK CONSTRAINT [FK_Table_3_Table_2]
GO
INSERT INTO [dbo].[Table_2]
([table2filler])
VALUES
('test')
print 'table2id:'
print scope_identity()
GO
declare #guid uniqueidentifier
set #guid=newid()
print 'table1guid:'
print #guid
INSERT INTO [dbo].[Table_1]
([table1guid])
VALUES
(#guid)
GO
now open a new web apps project, create a new dbml and drag&drop these 3 tables
now just put that code in a webpage codebehind
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim test As New Table_3
Dim db As New DataClasses1DataContext
test.table2id = 1
test.tableguid = New Guid("guid from table 1")
test.table3filler = "a"
db.Table_3s.InsertOnSubmit(test)
db.SubmitChanges()
End Sub
and run it
you will get an invalid cast error
only way so far for me to be able to run that code is to remove the link between the table inside the DBML
is there a way to do that insert without removing the link between the tables?
I actually created your database just as you specified and ran exactly this code it locally on my box. I get no such error when I substitute the a real GUID in this line:
test.tableguid = New Guid("guid from table 1")
Are you sure your GUIDs are in the right format? Are you sure your tables are created exactly like you specified? Double check it... My guess is that if you recreate this sample db from scratch, you won't see this problem.
I believe Linq2sql doesn't like it when you set a foreign key id directly. It prefers you to set the foreign object itself.
test.table_2 = db.Table_2.First(t2 => t2.table2id = 1);
test.tableguid = New Guid("guid from table 1")
test.table3filler = "a"
ok, it's in fact a bug with .net 3.5 and fixed with .net 4.0
but there is a hotfix, see detail here
everything work like it should after that hotfix is installed