Change A Database's Table's Name (SQL Server 2008) - database

I got a table named tblHello and I wanna rename it to Hello

Right click on the table and select rename in management studio
You can also use sp_rename:
sp_rename 'old_table_name', 'new_table_name'

I want to point out that table renaming is not as simple as just changing the name when you have queries written. You also need to change all references to the old name in every stored proc, view, function and dynamic sql code. This is not something to be taken on lighty in something that is already on production.
But #jonH has the answer for how to do it (you run that in the query window making sure you switch to the correct database first). Of course you have to have the right security permissions to rename objects.

If its a small project,
you can directly change it from User Interface.
In Server Explorer, right-click the table you want to rename and Open Table Definition.
Right-click the table in the Table Definition window and choose Properties from the shortcut menu.
In the field for the Name value in the Properties, type a new name for the table.
Save the table.
And its done.

Related

Set schema name in SqlUserDefinedTypeAttribute

When creating a UserDefinedType in C# code for the sake of SQLCLR integration, it is required that you prefix a class or struct with a SqlUserDefinedType, such as here:
[SqlUserDefinedType(
Name = "g.Options",
// ...
)]
public struct Options : INullable {
// ...
}
Notice that in the "Name" parameter, I attempt to set a schema in addition to the object name. But, when I generate the script in the publish stage of a Visual Studio Database Project, I get:
CREATE TYPE [dbo].[g.Options]
There is no "schema" parameter for SqlUserDefinedType.
I do believe I can write the T-SQL script to make the type from the assembly specifically, but I would like to avoid that, as I plan on putting most of my types in different schemas and wouldn't be happy to have to register via explicit TSQL on each one.
EDIT:
As Solomon Rutzky points out, you can set the Default Schema in the project properties. It is certainly no substitute for something akin to a 'schema' parameter in SqlUserDefinedType, particularly if you want to work with multiple schemas, but it certainly gets the job done for many people's needs.
A post-deployment script will technically get the job done, but unfortunately, the comparison engine doesn't know about the post-deployment logic and so will perpetually register the schema difference as something that needs to be changed. So all your affected objects will be dropped and re-created on every publish regardless of whether you changed them or not.
The Schema name is specified in a singular location per each project, not per object.
You can set it in Visual Studio via:
"Project" (menu) -> "{project_name} Properties..." (menu option) -> "Project Settings" (tab)
On the right side, in the "General" section, there is a text field for "Default schema:"
OR:
you can manually edit your {project_name}.sqlproj file, and in one of the top <PropertyGroup> elements (one that does not have a "Condition" attribute; the first such element is typically used), you can create (or update if it already exists) the following element:
<DefaultSchema>dbo</DefaultSchema>
HOWEVER, if you are wanting to set one object (such as a UDT) to a different Schema name than the rest of the objects are using, that would have to be done manually in a Post Release SQL script. You can add a SQL script to your project, then in the Solution Explorer on the right side, select the SQL script, go to its Properties, and for "BuildAction", select "PostDeploy". In that post-deploy script, issue an ALTER SCHEMA statement:
ALTER SCHEMA [g] TRANSFER TYPE::dbo.Options;

SQL Server changing data type from decimal(18,7) to decimal(23,15)

One of my fields has datatype decimal(18,7). I would like to change it to decimal(23,15). When I attempt it, get error message about "Saving changes not permitted.... require the following tables to be dropped and re-created.... can't be re-created or enable the option Preventing saving changes that rquire that table to be re-create"
Where do I change options to allow table to be dropped and re-created? I have nothing valuable in this table, I can easily drop the table manually and re-create it manually, but would rather have SQL Server do it for me.
Regards,
Well, as it says, it is in the options. Which are in the menu.
And in the documentation:
http://msdn.microsoft.com/en-us/library/bb895146.aspx
To change this option, on the Tools menu, click Options, expand
Designers, and then click Table and Database Designers. Select or
clear the Prevent saving changes that require the table to be
re-created check box.

How can I get the view to display the current column definition?

Using SQL Server Express 2008 & Management Studio.
I have a table & a view. At the time the View was created, the Table's FirstName column was varchar(50). Modify Table.Firstname to be nvarchar(50) and the View still indicates that the column is varchar(50).
How do I get the View to display the current column definition?
Here's what I've tried (that didn't work):
Click the Refresh button
Close Management Studio & re-open it.
Here's what did work:
Create a brand new view. The new view does display the current field definition
Note: This project has many views, so I'd prefer not to have to re-create all of them.
Run either sp_refreshview or the newer sp_refreshsqlmodule
The best solution is to use WITH SCHEMABINDING so this can't happen.
Quite common on SO, example here
Look up sys.sp_refreshsqlmodule in Books Online.

Some tables in SQL Server require [user].[table] and others don't, why is this and can I force it?

As the title suggests I am confused as to why some tables in my database fall over if you do something like;
SELECT * FROM [user].[table]
And yet on another tables it works fine.
I am testing some code that will eventually be on a server that cries if you don't use [user].[table] so I would really like to force this on my machine.
Could someone explain why this happens and possible show me how to fix it.
More Info
Here is the message I get when I try and run a query using [user].[table] instead of just [table];
[Microsoft][ODBC SQL Server
Driver][SQL Server]Invalid object name
'usr.tbl'
The "user" bit is the schema a table belongs to
So you can have dbo.table and user.table in the same database.
By default, SELECT * FROM table will usually look for the dbo.table. However, if the login/user has a different default schema then it will look for thatschema.table
To fix it:
You can use ALTER SCHEMA .. TRANSFER.. to fix the current setup
Ongoing, ensure every table reference has the correct schema on CREATE, ALTER, SELECT, whatever
Also see "User-Schema Separation" on MSDN
What you refer to as [user] is actually something called a schema. Every user has a default schema, which means that when you are logged in as that user you can refer to the tables in the default schema without the schema prefix. One way to solve this would to be to make sure that no user has the default schema where the tables are located. Basically you can just make an emptry schema and use that as the default schema for all your users.
Go to YourDatabase->Security->Users and see the properties (by right clicking) to change the default schema for your users.

PowerDesigner - Prevent DB name to be uppercase in generated SQL

I am currently beginning playing around with Sybase PowerDesigner and its Physical Data Model (PDM).
I have managed to create some entities/tables, columns, et cetera, and the case in the generated SQL (to create the database from the model) is like I want.
However, regarding the database name, PowerDesigner insists for it to be always in uppercase. Actually, I am not getting any error message or anything like that and I'm actually allowed to type in a mixed-case database name but as soon as I close the "Model Properties" window and come back to it to check, the database code name is back to uppercase.
I obviously unticked the "=" button connecting the logic name to the code name so I can type a different code name, but it still doesn't work (while it does for tables).
Is there any way to have a database name in the generated SQL script corresponding to the case I specified in PowerDesigner? The DBMS type is SQL Server 2008 (tried previous versions and the problem is the same).
Thanks a lot in advance.
I found how to do it:
Right-click on an empty spot in the model area.
Select "Model Options..." from the context menu.
Select "Naming Convention" from the tree.
Select "Code" from the tab group.
Select "Mixed case" beside "Character case".
Click OK.
Right-click on an empty spot in the model area.
Select "Model Options..." from the context menu.
Select "Naming Convention" from the tree.
Select "name to code" check "Apply name to code convertion"

Resources