How can i import indexes into ms SQL Server? - database

I created a new databases , and added tables using the import data wizard. But the wizard din't create the indexed and constraint's on the table. How can I Import indexes?

If your source is also SQL Server, you should be able to run Tasks -> Generate Scripts and select "Script Indexes" in the list of options for the old database and execute the script on the new database with maybe a change in database name.

Just manually add indexes to your table
Here is an example from MSDN:
This example creates an index on the au_id column of the authors table.
SET NOCOUNT OFF
USE pubs
IF EXISTS (SELECT name FROM sysindexes
WHERE name = 'au_id_ind')
DROP INDEX authors.au_id_ind
GO
USE pubs
CREATE INDEX au_id_ind
ON authors (au_id)
GO
The other way you can do this is open the table in design mode management studio, highlight the field you want to index and look at the options at the top. One of them is for indexes and you can simply manually add it and give it a name right in management studio.

Related

in SQL Server 2014 why SELECT * FROM System_User doesn't work but SELECT * FROM [ProjectManage].[dbo].[System_User] work

I am using SQL Server 2014 management studio, I found a strange situation.
in a SQL query dialog, I key in the below SQL:
SELECT * FROM System_User
I can see the table name in resource management dialog is: dbo.System_User
Then when I try to execute it, it said there is some grammar error.
If I change the SQL as:
SELECT * FROM [ProjectManage].[dbo].[System_User]
it works well.
But acutally in same SQL query dialog, I have another SQL:
SELECT * FROM FlowTemplateTransition
and its table name is dbo.FlowTemplateTransition in resource management dialog.
My question is:
what is the difference between dbo.FlowTemplateTransition and FlowTemplateTransition?
when do I need to add dbo and when it is unnecessary?
do I need to using square brackets always?
Thanks
dbo is the default schema in SQL Server. You can create your own schemas to allow you to better manage your object namespace.
In SQL Server Management Studio, you can create your own schema by browsing to Databases - Your Database - Security - Schemas. Or you can execute below query to create a schema.
CREATE SCHEMA [SchemaName] AUTHORIZATION [dbo]
You can use them to logically group your tables, for example by creating a schama for "vendors" information and another for "customers" data. Your tables would then display as:
vendors.BankAccounts
vendors.Transactions
customers.Address
Instead of using the default schema of dbo.
No need to use the square brackets[] always. But if your database or table name contains something like a dot(.) then you must use [].
As you have 3 questions. I have list it down separately.
Why SELECT * FROM System_User not working?
what is the difference between dbo.FlowTemplateTransition and FlowTemplateTransition
when do I need to add dbo and when it is unnecessary? do I need to using square brackets always?
Answer 1 : System_User is sql server built in function. Your table name conflict with this function that is why it gives an error.
Answer 2 : dbo is default database owner. If you don't write owner of table than Sql Server automatically consider dbo as an owner for that table.
Answer 3 : Generally dbo is not necessary to add.
The brackets are required if you use keywords or special chars in the table name.

Updating a table from values in an excel file

I have a set of excel files with a total of around 130,000 lines. Each line has a column with an ID and a column with a name. I need to update an existing column in one table in the database and fill each ID row with its matching name.
This only needs to be done once so I was going to just use a formula in excel to make each line a query (=CONCATENATE("UPDATE Table SET Name = '", $C1, "' WHERE ID = ", $A1)) then copy all of those queries out and run them in Sql Server Management studio. Is this an OK way to do it or will the server choke on 130,000 individual queries?
What is the proper way to do it?
Thanks!
Import your Excel workbook to a new table, then join that with your existing table on the ID field and build an update query from that.
Create an SSIS package to import the data. You can have Sql Server create the SSIS package for you by right clicking on the target database name, then select "Task" from the pop up window, and then select import data. Follow the GUI and in the first window select "Microsoft Excel" as the data source.

SQL Server - Script to update Full text Catalogue Index

I have a full text search index in the development environment. It contains data of the table to be indexed and the coulmns selected for the full text indexing. I viewed it through Management Studio.
I need to update the selected columns of the index in the acceptance environment. Can I generate a script for this purpose.
I tried generating a script of the index by goining into "Storage" > Under "Full text Catalogs" > Right click on the Index > select "Script Catalog as" > select "Drop and Create to". But it gives a very basic script, The columns of the table which I need to associate in the index are not generated in the script. The script i get is as below:
DROP FULLTEXT CATALOG [MYTABLE]
GO
CREATE FULLTEXT CATALOG [MYTABLE]WITH ACCENT_SENSITIVITY = OFF
GO
But what I need is to change the selected columns of the table in the index.
You can generate the script for the full text index by scripting the table, but first you need to enable the Script full-text indexes option as follows:
In SQL Server Management Studio, click the Tools menu > Options
In the left pane select SQL Server Object Explorer > Scripting
In the right pane under Table and view options, set Script full-text indexes to True.
Once that option is set, right-click the table and select Script Table as > CREATE To (or DROP and CREATE To). The relevant part of the script looks something like this:
CREATE FULLTEXT INDEX ON [dbo].[Table1] (
[Column1] LANGUAGE 'Neutral'
)
KEY INDEX [PK_MyTable] ON ([MYTABLE], FILEGROUP [PRIMARY])
WITH (CHANGE_TRACKING = AUTO, STOPLIST = SYSTEM)
GO
(where MYTABLE is the name of the full text catalog, based on the original question)

How to merge table from access to SQL Express?

I have one table named "Staff" in access and also have this table(same name) in SQL 2008.
Both table have thousands of records. I want to merge records from the access table to sql table without affecting the existing records in sql. Normally, I just export using OCBC driver and that works fine if that table doesn't exist in sql server. Please advise. Thanks.
A simple append query from the local access table to the linked sql server table should work just fine in this case.
So, just drop in the first (from) table into the query builder. Then change the query type to append, and you are prompted for the append table name.
From that point on, just drop in the columns you want (do not drop in the PK column, as they need not be used nor transferred in this case).
You can also type in the sql directly in the query builder. Either way, you will wind up with something like:
INSERT INTO dbo_custsql
( ADMINID, Amount, Notes, Status )
SELECT ADMINID, Amount, Notes, Status
FROM custsql1;
This may help: http://www.red-gate.com/products/sql-development/sql-compare/
Or you could write a simple program to read from each data set and do the comparison, adding, updating, and deleting, etc.

SQL Server: Copying table contents from one database to another

I want to update a static table on my local development database with current values from our server (accessed on a different network/domain via VPN). Using the Data Import/Export wizard would be my method of choice, however I typically run into one of two issues:
I get primary key violation errors and the whole thing quits. This is because it's trying to insert rows that I already have.
If I set the "delete from target" option in the wizard, I get foreign key violation errors because there are rows in other tables that are referencing the values.
What I want is the correct set of options that means the Import/Export wizard will update rows that exist and insert rows that do not (based on primary key or by asking me which columns to use as the key).
How can I make this work? This is on SQL Server 2005 and 2008 (I'm sure it used to work okay on the SQL Server 2000 DTS wizard, too).
I'm not sure you can do this in management studio. I have had some good experiences with
RedGate SQL Data Compare in synchronising databases, but you do have to pay for it.
The SQL Server Database Publishing Wizard can export a set of sql insert scripts for the table that you are interested in. Just tell it to export just data and not schema. It'll also create the necessary drop statements.
One option is to download the data to a new table, then use commands similar to the following to update the target:
update target set
col1 = d.col1,
col2 = d.col2
from downloaded d
inner join target t on d.pk = t.pk
insert into target (col1, col2, ...)
select (d.col1, d.col2, ...) from downloaded d
where d.pk not in (select pk from target)
If you disable the FK constrains during the 2nd option - and resume them after finsih - it will work.
But if you are using identity to create pk that are involves in the FK - it will cause a problem, so it works only if the pk values remains the same.

Resources