SQL Schema changed to be "dbo.dbo." after altering table name - sql

I tried to alter a table with a new table name. I successfully changed the name but the schema also changed from "dbo." to be "dbo.dbo.". When I tried selecting data from the new table, it shows in the Message box that the table is invalid so I can't do anything with the new table.
Does anyone know how can I change the table back to the original schema? I used WINSQL with sql server 2008. Thanks,

My guess is that you've actually simply renamed the table to [dbo.tablename] and its fully qualified name is [dbname].[dbo].[dbo.tablename]. This happens when you right-click to rename a table name in SSMS and I'd imagine that WinSQL is doing the same thing (though I don't know why you're using that tool when SSMS is included). When you right-click, it takes away the schema name which makes you believe you need to fully qualify the new name, but you don't.
You should be safe to right-click and rename the table name to just the new table name.
To be sure, though, you can run:
select *
from sys.schemas
where name = 'dbo.dbo';
just to confirm that you've not created a new schema.
EDIT
Just for the sake of completeness I'll incorporate the comment made by #billinkc:
Run this query to get the exact schema of the table:
select
s.name as SchemaName,
t.name as TableName
from sys.schemas s
join sys.tables t
on s.schema_id = t.schema_id
where t.name = 'tablename'

Related

Altering the Data Type of Table and View

I am having a big database with lots of view and tables.
now in many tables and views the datatype of column , named 'Company' is INT
with time the data changed and now we want 'Company' column to hold character value.
Can I write a cursor to change the datatype of all this tables and Views, Because manually changing the datatype is time consuming.
I tried modifying datatype with the help of this link: How to Change All Sql Columns of One DataType into Another
But this is working only for table , I am not able to change the datatype of Views
Thanks in advance for your help!
If you do not see the correct data type in your views, then you need to recompile them. SQL Server is not very smart about invalidating views if dependent objects change. E.g. if you have a view that select * (which you anyways should not do) from a table and you add a column, the view will not reflect the new column until you recompile it. The same is true for other changes to dependent objects.
To recompile the view use the stored procedure sp_refreshview. This MSDN page describes the procedure and also has a script at the bottom of the page that allows you to bulk refresh all your views.
First of all, you don't need to modify anything in view. Any change in corresponding table will automatically reflect in your view.
If not, it might be because if a view is not created with schemabinding, sp_refreshview should be run when changes are made to the objects underlying the view that affect the definition of the view. Otherwise, the view might produce unexpected results when it is queried. SO as suggested by #Ralf, I +1 that answer, use sp_refreshview to update view.
For your comment ALTER TABLE ALTER COLUMN Company failed because one or more objects access this column. This is because some constraint or index is using that column you have to first drop that and then only you might be able to alter that column.
You can find related constraint using query:
select db_name() as CONSTRAINT_CATALOG
,t_obj.name as TABLE_NAME
,user_name(c_obj.uid) as CONSTRAINT_SCHEMA
,c_obj.name as CONSTRAINT_NAME
,col.name as COLUMN_NAME
,col.colid as ORDINAL_POSITION
,com.text as DEFAULT_CLAUSE
from sysobjects c_obj
join syscomments com on c_obj.id = com.id
join sysobjects t_obj on c_obj.parent_obj = t_obj.id
join sysconstraints con on c_obj.id = con.constid
join syscolumns col on t_obj.id = col.id
and con.colid = col.colid
where
c_obj.uid = user_id()
Drop any index and/or constraint on column & then try alter, it must work then.
Also refer answers to this question Drop a column from table problem (SQL Server 2008)
Hope it helps.

How do I get a list of columns in a table or view?

On occasion, I'm interested in getting a list of columns in one of the tables or views in my SQL Server 2008 R2 database. It's useful, for example, if you're building database documentation without using an expensive off-the-shelf product.
What's an easy way to get this information?
In SQL Server 2008 R2 (among other versions), there are system views provided automatically with every database. As long as you are connected to the database where your table resides, you can run a query like this:
DECLARE #TableViewName NVARCHAR(128)
SET #TableViewName=N'MyTableName'
SELECT b.name AS ColumnName, c.name AS DataType,
b.max_length AS Length, c.Precision, c.Scale, d.value AS Description
FROM sys.all_objects a
INNER JOIN sys.all_columns b
ON a.object_id=b.object_id
INNER JOIN sys.types c
ON b.user_type_id=c.user_type_id
LEFT JOIN sys.extended_properties d
ON a.object_id=d.major_id AND b.column_id=d.minor_id AND d.name='MS_Description'
WHERE a.Name=#TableViewName
AND a.type IN ('U','V')
Of course, this is just a starting point. There are many other system views and columns available in every database. You can find them through SQL Server Management Studio under Views > "System Views
Another way is querying the INFORMATION_SCHEMA.columns view as detailed here:
Information_Schema - COLUMNS
This will give you information for all the columns in the current database (and what table/view they belong to) including their datatypes, precision, collation and whether they allow nulls etc
Usefully as well, these views are maintained in multiple DBMS programs too, so you could potentially use the same or similar query to get the same information regarding a MySQL database as you can a SQL Server DB, which could be useful if you are developing on multiple platorms.
sp_columns returns detailed information about each of the columns in the table. SO Answer
sp_columns #tablename
sp_help returns detailed information about the entire table including the columns and constraints. SO Answer
sp_help #tablename
To get a list of Columns of a view with some other information about the column you can use the following:
SELECT * FROM sys.columns c, sys.views v
WHERE c.object_id = v.object_id
AND v.name = 'view_Name'
GO
And if you only want the list of Column Name use this.
SELECT c.name
FROM sys.columns c, sys.views v
WHERE c.object_id = v.object_id
AND v.name = 'view_UserAssessphers'
GO
exec sp_helptext <your view name>
Also works for the view only, blachniet's answer is best if you need details on the columns in the table.
In a new query window, type the name of the view/table, highlight it, and press Alt-F1. This will run sp_help, like blachniet suggested.
simple list of column names without any further information.
SELECT COLUMN_NAME FROM TABLENAME.INFORMATION_SCHEMA.COLUMNS;
Replace TABLENAME with your tables name.

How do I search for a table in sql 2005?

I just created a table within a huge database and saved it. I've tried to refresh the database to see if my new table appears and I closed & reopened the Management Studio but don't see my new table. I was wondering if there is a way to search for my new created table? I'm using SQL Server 2005.
Thanks a lot.
Select * from sys.tables where name like '%tablename%'
You can try this:-
SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[table_name_here]')
or try this:-
USE YourDBName
GO
SELECT *
FROM sys.Tables
where name like '%tablename%'
GO
If table doesn't appear, check under different schema.
select * from sys.all_objects where name like '%tableName%' and type = 'U'

Where does the Table Type gets stored in sql server? And how can we alter it?

I was reading about Table Value Parameter which seems interesting.. but i want to know that when we define a table type in database where does it get saved? What is its scope? And How can i modify that type .. suppose i created a table type as...
CREATE TYPE DeptType AS TABLE
(
DeptId INT, DeptName VARCHAR(30)
);
Now how can i get the schema of DeptType in future? In case of stored procedure its as simple as sp_helptext [ProcedureName]. How can i modify it? What is the scope of this type?
Try this query -
SELECT *
FROM sys.columns c
JOIN sys.table_types tt ON c.[object_id] = tt.type_table_object_id
WHERE tt.name = 'DeptType'
SELECT *
FROM sys.objects o
WHERE o.[type] = 'TT'
AND o.name LIKE 'TT_DeptType%'
How to modify -
Database Explorer ->
<your db> ->
Programmability ->
Types ->
User-Defined Table Types -> DROP-CREATE script
What is its scope?
It belongs to a schema, the same as a table, view, stored procedure, etc, would. For usage, it can only be used within the same database as it resides. There's no way to reference a table type from another database (even within the same instance), and two table types declared identically in separate databases are not interchangable.
And How can i modify that type
You cannot, unfortunately. The pattern to SQL commands is that if CREATE is used to bring an object into existence, ALTER is used to modify that object (And DROP to remove it). By looking at the ALTER statements, you'll note that there is no ALTER TYPE.
The best you can do is DROP the type and CREATE it again with its altered form. Unfortunately, to do so you have to drop all dependent objects first, then drop the type, then create it, then recreate all dependent objects.

recreating whole SQL Server Database with relations

i have used SQL server 2012 and made a huge database with many relations. today i realized using "NCHAR" instead on "NVARCHAR" adds space on end of strings which i don't like it. is there any way to recreate database with relations. I've tried "drop and create" for tables but needs to remove all relations and define them again which is very time consuming.
It's possible to alter only the affected tables? like this:
SELECT 'alter table '+s.name+'.'+t.name+
' alter column '+c.name+' nvarchar('+convert(varchar(11),c.max_length/2)+') go'
FROM sys.tables as T
inner join sys.schemas as s
on T.[schema_id]=s.[schema_id]
inner join sys.columns as C
on T.[object_id]=C.[object_id]
and c.system_type_id=239--nchar type
, and before that don't forget to use the same kind of mechanism to generate an update statement to remove extra spaces.