I am using a SQL Server 2008 database.
I have two databases namely db1 and db2. In both there is a table tblcountry. I create this on 1st database. Then how can I script with its data for create on 2nd database?
I use the code below
CREATE TABLE [dbo].[tblCountry]
(
[record_Id] [int] IDENTITY(1,1) NOT NULL,
[country] [nvarchar](150) NULL,
[nationality] [nvarchar](150) NULL,
[lsdMdfdOn] [datetime] NULL,
[lstMdfdBy] [nvarchar](350) NULL,
[isDeleted] [bit] NULL,
[isEnabled] [bit] NULL,
)
Then what code will I use for the getting include the data?
No, you cannot view the data if you are using the create query.
If you want to see the data of the table on second database then you can use this query on your second database db2
select * from [db1].[dbo].[tblCountry]
But you can not view the data and create query at the same time.
Although it may seem very wierd solution but I guess what you can do is you can copy the create query on the query analyzer window and beneath that write the select query and execute it. (But I guess this is how most of the programmers do that)
If you are on the same server or have a linked server:
CREATE TABLE tblCountry
SET IDENTITY_INSERT tblCountry ON
INSERT INTO [database2].tblCountry
SELECT * FROM [database1].tblCountry
SET IDENTITY_INSERT tblCountry OFF
The most Easy way for this problem is for you to
Right Click on the Database on the Object Explorer
click Generate Scripts
on the introduction click Next
Select radio button on Script entire database and all database
objects or you can just select specific tables or stored
procedures by selecting the other radio button
On the Set Scripting Options click on Advanced Select the things you
want to script
Then on the Query just change the database name after the query USE
db2
Right click on database and click on tasks and export data
you can use export data option in sql server...it will give you data with table script
Related
I have an Access database that uses SQL Server as its backend. One table is the data for a subform which is loaded into a parent form and displayed in the parent forma as a datasheet. The datasheet is intended to have rows added to it by users.
The subform backend table is defined in SQL Server with a primary key [ID]. The primary key is linked to the Master ID of the current record displayed in the parent form. The subform is then filtered by the relationship between the subform table primary key and the parent form master ID. Nothing unusual here.
The problem that when the parent form is first opened in Access the subform is cannot have records added. It is not until the linked table manager is opened and the backend table is RELINKED can the user add records to the subform. Oddly enough, when relinking the table I am always prompted for the primary key of the table even though it is defined in SQL Server.
The subform has the following settings
Data Entry: Yes
Allow Additions: Yes
Allow Deletions: Yes
Allow Edits: Yes
Allow Filters: Yes
Record Locks: No Locks
Here is the CREATE TABLE for the backend table:
CREATE TABLE [dbo].[ProjectHealth](
[ID] [int] IDENTITY(1,1) NOT NULL,
[Project Health] [nvarchar](2000) NULL,
[Timestamp] [datetime] NULL,
[ProjectID] [int] NULL,
[Entered By] [nvarchar](255) NULL
) ON [PRIMARY]
GO
Again, the RELINK solves the problem. But a VBA TableDef.Refreshlink call does not do the trick. Even if I reset the primary key from VBA.
My suspicion is that I have modified some value in the subform settings that have caused this problem which started happening after a few days of normal behavior. Thanks in advance for any suggestions you can provide.
The creation of a SQL Server External Table requires remote tables to be made accessible by running code of the following format on the local database:
CREATE EXTERNAL TABLE [TABLE_NAME]
(
[Id] [int] NOT NULL,
[Name] [int] NULL,
)
WITH (DATA_SOURCE = [EXTERNAL_DB_NAME])
I have a quite a few external tables to work with, and am wondering if this functionality can be automated. So, is there a way to create the text for a CREATE TABLE statement from the Information Schema views on the remote database ? This could then be altered and used to populate a dynamic query to be run on the local database.
project->add new item->data->service based database->(name of DB)->add
although the DB has been created,and database and its' table's folder are in the server explorer, it is not allowing to create a new table by right clicking on table folder.It's only giving refresh and properties as options. why is that?? what can i do to crate a new table?
To create the table:
In Server Explorer or Database Explorer, expand the Data Connections node, and then expand the SampleDatabase.mdf node.
If the explorer for your version of Visual Studio isn't open, choose View, Server Explorer or View, Other Windows, Database Explorer on the menu bar.
Open the shortcut menu for Tables, and then choose Add New Table.
The Table Designer opens and shows a grid with one default row, which represents a single column in the table that you're creating. By adding rows to the grid, you'll add columns in the table.
In the grid, add a row
Go to sql server studio>create new query window then write script like this to create a table
CREATE TABLE [schema].[sampletable](
[RecordNo] [int] NOT NULL,
[ID_NO] [nvarchar](50) NOT NULL,
[NAME] [nvarchar](200) NULL,
[Status] [varchar](20) NULL
)
When I try to script a table CREATE from a database, it doesn't always script all the columns inside a single CREATE TABLE. Some of the columns will be added via ALTER TABLEs. Why is this? Is SQL server capturing the history of the schema modifications, and any columns added after the initial creation are scripted with ALTERS? The reason this matters is because I'm scripting tables to be used within a VS Database project and it can't handle the ALTER statements.
CREATE TABLE [dbo].[tblPackageType]
(
[PackageTypeId] [int] IDENTITY(1,1) NOT FOR REPLICATION NOT NULL,
[PackageTypeDesc] [varchar](50) NOT NULL
) ON [PRIMARY]
ALTER TABLE [dbo].[tblPackageType] ADD [EDIcode] [varchar](10) NOT NULL
ALTER TABLE [dbo].[tblPackageType] ADD [EDI211] [varchar](10) NULL
Is SQL server capturing the history of the schema modifications, and any columns added after the initial creation are scripted with ALTERS?
Yes.
If you want to get around this, you can alter the script to be a create including all the needed columns, and after you run it the updated definition will be saved.
I have a database that the client needs to update. They like to use access. Some tables randomly become read-only for them. Any ideas why?
They are using Access 2007 and MS SQL 2005.
SQL Table:
CREATE TABLE [dbo].[Users](
[SyncGroup] [varchar](20) NULL,
[UserID] [varchar](20) NOT NULL,
[Password] [varchar](20) NOT NULL,
[Restriction] [text] NULL DEFAULT (' '),
[SiteCode] [varchar](20) NULL,
[Group] [varchar](20) NULL,
[EmpId] [varchar](20) NULL,
[TimeZoneOffset] [int] NULL,
[UseDaylightSavings] [bit] NULL,
PRIMARY KEY ([UserID]) )
Access really likes having a TimeStamp aka RowVersion field on every table. I don't know if this will fix your problem though.
"On servers that support them (such as Microsoft SQL Server), timestamp fields make updating records more efficient. Timestamp fields are maintained by the server and are updated every time the record is updated. If you have a timestamp field, Microsoft Access needs to check only the unique index and the timestamp field to see whether the record has changed since it was last retrieved from the server. Otherwise, Microsoft Access must check all the fields in the record. If you add a timestamp field to an attached table, re-attach the table in order to inform Microsoft Access of the new field."
http://technet.microsoft.com/en-us/library/cc917601.aspx
are users accessing the database while you're trying to do stuff iwth sql? if so, then you will get an error message stating that the database is in use and is read only. no one can be in the database when you are doing things with it though sql.
Sounds like a permissions problem. Are you keeping careful track of who is altering the schema? You may have users who aren't permitted to use changes made by certain other users.