TSQL: How to check if column is fulltext enabled? - sql-server

I need to modify a column definition, but I would like to check if the column is full text enabled first. Is there a way to do such a check in a TSQL script?
I'm using SQL Server 2005.

You could try using the COLUMNPROPERTY() function.
DECLARE #value INT;
SELECT #value = COLUMNPROPERTY(OBJECT_ID('schema.table'), 'column_name', 'IsFulltextIndexed')
IF (#value = 1)
PRINT 'Fulltext column'
ELSE
PRINT 'No Fulltext column'

You can try something like this:
SELECT *
FROM sys.columns c
INNER JOIN sys.fulltext_index_columns fic ON c.object_id = fic.object_id
AND c.column_id = fic.column_id
If you need to limit it to a given table, use this:
SELECT *
FROM sys.columns c
INNER JOIN sys.fulltext_index_columns fic ON c.object_id = fic.object_id
AND c.column_id = fic.column_id
WHERE c.object_id = OBJECT_ID('YourTableNameHere')

Related

Check If column exists or not in table and rename them

I am trying to merge two different rules into one that should run in two databases server.
I did merged the files. but i am stuck at the this columns.
There are two columns with different names but the same data in the two databases. I want my query to check if if this column exist
First column - APE.RecProdOwner
Second Column - APE.ReconciliationOwner
I want my query to check for both these columns in the database and if one of the two exists then it should store the data into the new column name as “RecProdOwner”
You can use an EXISTS against the sys objects, and then simply use sys.sp_rename to rename the column:
USE Sandbox;
GO
CREATE TABLE dbo.YourTable ([APE.RecProdOwner] int,
ReconciliationOwner int);
GO
IF EXISTS (SELECT 1
FROM sys.schemas s
JOIN sys.tables t ON s.schema_id = t.schema_id
JOIN sys.columns c ON t.object_id = c.object_id
WHERE s.name = N'dbo'
AND t.name = N'YourTable'
AND c.name = N'APE.RecProdOwner')
EXEC sys.sp_rename N'dbo.YourTable.[APE.RecProdOwner]','RecProdOwner','COLUMN';
IF EXISTS (SELECT 1
FROM sys.schemas s
JOIN sys.tables t ON s.schema_id = t.schema_id
JOIN sys.columns c ON t.object_id = c.object_id
WHERE s.name = N'dbo'
AND t.name = N'YourTable'
AND c.name = N'APE.ReconciliationOwner')
EXEC sys.sp_rename N'dbo.YourTable.[APE.ReconciliationOwner]','ReconciliationOwner','COLUMN';
GO
SELECT *
FROM dbo.YourTable;
GO
DROP TABLE dbo.YourTable;

Need to change GUID to varchar or char

I am trying to use a reference table to update a code, but I am getting the error:
Conversion failed when converting from a character string to
uniqueidentifier
The ID is a guid: 086B9FE7-3980-47D7-BB05-003708F1D564 and the reference code I want to use is 4 characters, like H100.
I received the initial error when I tried to alter the datatype in the table and then update the file with the values from the reference table. I have tried converting and casting based on other articles, but even if I cast or convert successfully, I still get the same message.
RefTable:
Id | ReportCode
6340FCEA-161C-42F4-8D7F-46B4C2E6C4E2 | H100
DataTable:
CauseId
6340FCEA-161C-42F4-8D7F-46B4C2E6C4E2
Code I am using to try and update. The first works, and the second bring the error msg:
select cast(nvarchar(36), ID) as ID
from [dbo].[reftable]
UPDATE dbo.datatable
SET causeid = L.reportcode
FROM dbo.datatable S
join dbo.reftable L on S.causeid = L.id
Uniqueidentifier is a particular column type that holds 36 bytes, with some hyphen characters in the middle. Asigning a string value that doesn't match it's format will always fail when trying to convert it.
-- Conversion failed when converting from a character string to uniqueidentifier.
SELECT CONVERT(UNIQUEIDENTIFIER, 'H100')
-- OK
SELECT CONVERT(UNIQUEIDENTIFIER, 'c029f8be-29dc-41c1-8b38-737b4cc5a4df')
If you want to use a common VARCHAR as your new causeid value, you need to first convert the column type to VARCHAR (or NVARCHAR).
ALTER TABLE dbo.datatable ALTER COLUMN causeid VARCHAR(200) -- NOT NULL if you need
If you can't alter the data type it's probably because there is an INDEX or a CONSTRAINT linked to it. When you try to alter, the SQL engine will tell you which object is linked to it. You will have to drop them, alter the column type and then create them again.
I use these queries to check indexes and constraints. I updated the search values for your table.
DECLARE #table_name VARCHAR(200) = 'datatable'
DECLARE #column_name VARCHAR(200) = 'causeid'
-- Indexes
SELECT
SchemaName = SCHEMA_NAME(t.schema_id),
TableName = t.name,
IndexName = ind.name,
IndexType = CASE ind.index_id WHEN 0 THEN 'Heap' WHEN 1 THEN 'Clustered' ELSE 'Nonclustered' END,
Disabled = ind.is_disabled,
ColumnOrder = ic.index_column_id,
ColumnName = col.name,
ColumnType = y.name,
ColumnLength = y.max_length,
ColumnIncluded = ic.is_included_column
FROM
sys.indexes ind
INNER JOIN sys.index_columns ic ON ind.object_id = ic.object_id and ind.index_id = ic.index_id
INNER JOIN sys.columns col ON ic.object_id = col.object_id and ic.column_id = col.column_id
INNER JOIN sys.tables t ON ind.object_id = t.object_id
INNER JOIN sys.types y ON y.user_type_id = col.user_type_id
WHERE
t.is_ms_shipped = 0 AND
t.name = #table_name AND
col.name = #column_name
ORDER BY
SchemaName,
t.name,
ind.name,
ic.index_column_id
-- Constraints
SELECT
TableName = t.Name,
ColumnName = c.Name,
dc.Name,
dc.definition
FROM
sys.tables t
INNER JOIN sys.default_constraints dc ON t.object_id = dc.parent_object_id
INNER JOIN sys.columns c ON dc.parent_object_id = c.object_id AND c.column_id = dc.parent_column_id
WHERE
t.name = #table_name AND
c.name = #column_name
ORDER BY
t.Name

Get column stats in SQL Server 2008

I am trying to determine two things in a SQL Server 2008 database.
First, I need to know the columns that have null values in them.
Second, I need to know the count of nulls per column in the actual tables.
I know I can get the first one by doing:
SELECT t.name, c.name
FROM sys.tables t
INNER JOIN sys.columns c ON t.object_id = c.object_id
WHERE c.is_nullable = 1
I am struggling to find the second part though.
Say you have a table atable with columns not_nullable and is_nullable. As count does not count anything - you could instead count only where the columns is null
SELECT count(*) count_nulls FROM atable WHERE is_nullable is null;
Now how to do that for all nullable columns?
I came up with this - but its veeeery slow. But then again - how often do you need to do that?
create table #nullcolumns(nullable_column varchar(255), count_nulls int);
declare #sqladd nvarchar(1000);
DECLARE users_cursor CURSOR FOR
SELECT /* TOP 20 */ 'INSERT INTO #nullcolumns SELECT '''+
t.name+'.'+c.name+''' as nullable_column, count(*) from '
+t.name+' WHERE '+c.name+' IS NULL '
FROM sys.tables t
INNER JOIN sys.columns c ON t.object_id = c.object_id
WHERE c.is_nullable = 1
OPEN users_cursor
FETCH NEXT FROM users_cursor
INTO #sqladd
WHILE ##FETCH_STATUS = 0
BEGIN
exec sp_executesql #sqladd;
print #sqladd;
FETCH NEXT FROM users_cursor
INTO #sqladd
END
CLOSE users_cursor
DEALLOCATE users_cursor
SELECT * FROM #nullcolumns;

Query the contents of stored procedures on SQL Server

I am exploring a legacy database system and have very little knowledge of its internals. I would like to find all the stored procedures that invoke another stored procedure A.
How best to do this?
Can I write something like this pseudocode:
select name from AllStoredProcedures as Asp where Asp.TextualContent contains 'A'
Asp.TextualContent means the actual SQL contained in the SP.
SELECT OBJECT_NAME(object_id),
definition
FROM sys.sql_modules
WHERE objectproperty(object_id,'IsProcedure') = 1
AND definition like '%Foo%'
For SQL Server 2005/2008:
SELECT s.name SchemaName
,o.name RoutineName
,o.[type] RoutineType
,procs.*
FROM sys.sql_modules procs
INNER JOIN sys.objects o ON procs.object_id = o.object_id
INNER JOIN sys.schemas s ON o.schema_id = s.schema_id
WHERE procs.[definition] LIKE '%A%'
--AND o.[type] = 'P' --'P' for stored procedures
This query will retrieve the textual definition of stored procedures and filter using a simple wildcard.
For 2000 (untested, but IIRC it's the right table):
select p.[type]
,p.[name]
,c.[text]
from sysobjects p
join syscomments c
on p.object_id = c.id
where p.[type] = 'P'
and c.[text] like '%foo%'
For 2005:
select p.[type]
,p.[name]
,c.[text]
from sys.objects p
join sys.syscomments c
on p.object_id = c.id
where p.[type] = 'P'
and c.[text] like '%foo%'
For 2005 and 2008+
select p.[type]
,p.[name]
,c.[definition]
from sys.objects p
join sys.sql_modules c
on p.object_id = c.object_id
where p.[type] = 'P'
and c.[definition] like '%foo%'
Try This only one statement can solve your problem..
--note this does not show /r/n, it comes out as one long line
SELECT OBJECT_DEFINITION(OBJECT_ID(N'dbo.myStoredProc'))
or
DECLARE #objname nvarchar(max); -- note this truncates
SELECT #objname= OBJECT_DEFINITION(OBJECT_ID(N'dbo.myStoredProc'))
print #objname

Retrieve column descriptions from SQL Server-linked table in MS Access

I am linking to tables in SQL Server from an MS Access front-end. There are column descriptions for some of the tables in SQL Server that I would like to bring forward when I create the linked tables in Access. Is there a way to get at the column descriptions programmatically?
(I know how to append the description to the linked tables, I just need help getting at the descriptions in the back end.)
Try something like:
DECLARE #TableName varchar(100)
SELECT #TableName = 'yourtablename'
-- This will determine if we're using version 9 (2005) of SQL Server, and execute code accordingly
IF CAST(REPLACE(SUBSTRING(CAST(SERVERPROPERTY('productversion') as varchar),1,2), '.','') as int) >= 9
BEGIN
-- This is a SQL 2005 machine
SELECT
[Table Name] = OBJECT_NAME(c.object_id),
[Column Name] = c.name,
[Description] = ex.value
FROM
sys.columns c
LEFT OUTER JOIN
sys.extended_properties ex
ON
ex.major_id = c.object_id
AND ex.minor_id = c.column_id
AND ex.name = 'MS_Description'
WHERE
OBJECTPROPERTY(c.object_id, 'IsMsShipped')=0
AND OBJECT_NAME(c.object_id) = #TableName
ORDER
BY OBJECT_NAME(c.object_id), c.column_id
END
ELSE
BEGIN
-- assume this is a SQL 2000
SELECT
[Table Name] = i_s.TABLE_NAME,
[Column Name] = i_s.COLUMN_NAME,
[Description] = s.value
FROM
INFORMATION_SCHEMA.COLUMNS i_s
LEFT OUTER JOIN
sysproperties s
ON
s.id = OBJECT_ID(i_s.TABLE_SCHEMA+'.'+i_s.TABLE_NAME)
AND s.smallid = i_s.ORDINAL_POSITION
AND s.name = 'MS_Description'
WHERE
OBJECTPROPERTY(OBJECT_ID(i_s.TABLE_SCHEMA+'.'+i_s.TABLE_NAME), 'IsMsShipped')=0
AND i_s.TABLE_NAME = #TableName
ORDER BY
i_s.TABLE_NAME, i_s.ORDINAL_POSITION
END

Resources