How can i determine the name of the ROWGUIDCOL column in an SQL Server 2000 table?
i tried looking through syscolumns, e.g.:
SELECT *
FROM syscolumns
WHERE id = OBJECT_ID('Currencies')
But there is nothing there, or on the MSDN page that looks like rowguidcol.
Easy in SQL Server 2005
In SQL Server 2005 you just have to query sys.columns, e.g.:
SELECT *
FROM sys.columns
WHERE object_id = OBJECT_ID('Currencies')
AND is_rowguidcol = 1
Easy peasey.
You use COLUMNPROPERTY:
SELECT COLUMNPROPERTY(id, name, 'IsRowGuidCol'), * FROM syscolumns ... ;
In the format of the original question:
SELECT *
FROM syscolumns
WHERE id = OBJECT_ID('dbo.Currencies')
AND COLUMNPROPERTY(id, name, 'IsRowGuidCol') = 1;
For starters, if you don't know the rowguid column and you want to select it (but don't need to know its name for any other reason than to select it) you can do this:
SELECT $rowguid FROM YourTable;
If the table doesn't have a rowguid column:
In SQL 2000 you will get a column with name rowguid full of the value 0.00.
In SQL 2005 and up you will get an error, Invalid column name '$rowguid'.
For completeness note that in SQL 2005 and up you can also use $identity to get a table's identity column without knowing its name. In SQL 2000 you get Incorrect syntax near the keyword 'identity'.
Related
I'm using a query to create a new table in SQL Server using a select query.
My query is
create table test as
select id, sum(marks) as marks
from student
group by id
I'm getting error message in SQL Server. In Stackoverflow I found that instead of create table, SQL Server uses insert into. But I'm not getting how to write group by query using insert into. Can you please guide me?
Try this:
select id,sum(marks) as marks
INTO TEST
from student
group by id
Using INTO clause you are allowed to materialized the result in a table (it can be temporary table as well). There are few limitations - columns must have name (alias) and duplicate names are not allowed.
Select (columns Name or Functions (Max , Min , Sum ...) 1 , 2 , ...)
INTO (Destination table)
From (Source table)
GROUP BY (cloumns Name 1 , 2 , ...)
Can someone know if there is a way to find the column name that has the identity property in an sql server table
Thanks in advance
You can use sys.columns for this quite easily.
select *
from sys.columns
where object_id = object_id('YourTableNameHere')
AND is_identity = 1
As an alternative to looking it up in the metadata, you can use $identity to refer to an identity column. For example, given a table:
CREATE TABLE test_table (some_random_name INT IDENTITY, other_column VARCHAR(20));
...you can use:
SELECT $identity, other_column FROM test_table;
to bring back some_random_name, other_column.
($identity replaces the deprecated IDENTITYCOL of earlier versions of SQL Server.)
You can use the solution in this link to search the whole database for a specific value, which can be a username you know exists "somewhere" in the database.
It gives you the table and column name where that specific value exists.
Did the ordering of the returned columns change for select * for SQL Server 2012?
For example, for a table t with the columns a1,a2,a3 for which select * from t returned the columns in the order a1,a2,a3 for SQL Server versions prior to 2012, and after migrating to 2012 the order changed to some other order, e.g., a3,a1,a2.
Did anyone else notice this?
Why did this change?
I am personally curious as to why this happened. Maybe some problem with the conversion to SS 2012?
(No, my application does not depend on the column ordering and I know I should specify the columns I need in the order I want them instead of using select *)
It depends on each column's ordinal_position value in the information_schema.columns table.
Each column has an entry in this table with the table name and the ordinal position. Try this:
SELECT column_name, ordinal_position
FROM information_schema.columns
WHERE table_name = 'table name';
this should return the same as in the previous instance.
How can i set the collation SQL Server will use for the duration of that connection?
Not until i connect to SQL Server do i know what collation i want to use.
e.g. a browser with language fr-IT has connected to the web-site. Any queries i run on that connection i want to follow the French language, Italy variant collation.
i envision a hypothetical connection level property, simlar to SET ANSI_NULLS OFF, but for collation1:
SET COLLATION_ORDER 'French_CI_AS'
SELECT TOP 100 FROM Orders
ORDER BY ProjectName
and later
SELECT * FROM Orders
WHERE CustomerID = 3277
AND ProjectName LIKE '%l''ecole%'
and later
UPDATE Quotes
SET IsCompleted = 1
WHERE QuoteName = 'Cour de l''école'
At the same time, when a chinese customer connects:
SET COLLATION_ORDER Chinese_PRC_CI_AI_KS_WS
SELECT TOP 100 FROM Orders
ORDER BY ProjectName
or
SELECT * FROM Orders
WHERE CustomerID = 3277
AND ProjectName LIKE '學校'
or
UPDATE Quotes
SET IsCompleted = 1
WHERE QuoteName = '學校的操場'
Now i could alter every SELECT statement in the system to allow me to pass in a collation:
SELECT TOP 100 FROM Orders
WHERE CustomerID = 3278
ORDER BY ProjectName COLLATE French_CI_AS
But you cannot pass a collation order as a parameter to a stored procedure:
CREATE PROCEDURE dbo.GetCommonOrders
#CustomerID int, #CollationOrder varchar(50)
AS
SELECT TOP 100 FROM Orders
WHERE CustomerID = #CustomerID
ORDER BY ProjectName COLLATE #CollationOrder
And the COLLATE clause can't help me when performing an UPDATE or a SELECT.
Note: All string columns in the database all are already nchar, nvarchar or ntext. i am not talking about the default collation applied to a server, database, table, or column for non-unicode columns (i.e. char, varchar, text). i am talking about the collation used by SQL Server when comparing and sorting strings.
How can i specify per-connection collation?
See also
Similar question, but for ADO.net and connection strings
Similar question, but for ASP.net MVC2 and MySQL
1 hypothetical sql that exhibits locale issues
As marc_s commented, the collation is a property of a database or a column, and not of a connection.
However, you can override the collation on statement level using the COLLATE keyword.
Using your examples:
SELECT * FROM Orders
WHERE CustomerID = 3277
AND ProjectName COLLATE Chinese_PRC_CI_AI_KS_WS LIKE N'學校'
UPDATE Quotes
SET IsCompleted = 1
WHERE QuoteName COLLATE Chinese_PRC_CI_AI_KS_WS = N'學校的操場'
Still, I cannot find a statement on using COLLATE with a dynamic collation name, leaving as only possible solution dynamic SQL and EXEC. See this social.MSDN entry for an example.
i have 2 datbases A and B with tables AC and BD.table AC has a column ACcol and BD table has a column BDcol.
i want to move all the records in ACcol to BDcol with ACcol='admin'.
can anyone help me in doing this?
Assuming you have an account which has access to both databases, and the tables have the same schema, try the following :-
INSERT INTO B.dbo.BD
SELECT * FROM A.dbo.AB a WHERE A.dbo.AB.col = 'admin'
e.g.
INSERT INTO B.dbo.BD ( col1, col2, col3 )
SELECT col1, col2, col3 FROM A.dbo.AB a WHERE A.dbo.AB.col = 'admin'
The key is to properly qualify the databases you are using.
You should have a look at Sql Server Four Part Naming
SQL Server Four-part naming
Transact-SQL Syntax Conventions
(Transact-SQL) : Multipart Names
Using Synonyms in SQL Server
2005
SQL SERVER – 2005 – Introduction and
Explanation to SYNONYM – Helpful
T-SQL Feature for Developer