How to fetch row one by one in sql - sql

I am fetching table-names from particular database like this
SELECT name FROM sys.Tables where name like 'some pattern'
Output :
Name
sampletable_123,
sampletable_456,
sample_789.
It can return more than one row like above output. How to fetch row one by one?
Because I want to find out column-name/date from obtained table-name. If date is less than current date, I want to drop that table.
select top 1 Udate from sampletable_123
where Udate < convert(varchar(10),getdate(),101)
delete table sampletable_123.
How to do that?

This will generate dynamic sql script which you can preview and if those are the tables you want to delete you can use EXEC on it.
DECLARE #SQL NVARCHAR(MAX)
SET #SQL = ''
SELECT
#SQL += 'IF (OBJECT_ID(' + name + ') IS NOT NULL AND NOT EXISTS(SELECT TOP 1 uDate FROM ' + name + ' WHERE DATEDIFF(day,uDate,GETDATE()) != 0)
DROP TABLE ' + name + CHAR(10)
FROM sys.Tables
where name like 'some pattern'
PRINT #SQL --print will show you the delete query that was generated
--keep in mind that print wont be able to print the whole query if its bigger than 4000 characters
--but exec will still run it
--another way to preview it is to select the query because select has no limit it's just on one row
--EXEC(#SQL)

You can use a cursor on the above select, and fetch one row at a time.
For each row, you can retrieve the first row of the table and decide to drop it or not.
DECLARE #TABLES CURSOR
DECLARE #MYNAME VARCHAR(100)
SET #TABLES CURSOR FOR
SELECT name FROM sys.Tables WHERE name LIKE 'sample%'
OPEN #TABLES
WHILE 1 = 1 BEGIN -- INFINITE LOOP
FETCH NEXT FROM #TABLES INTO #MYNAME
IF ##FETCH_STATUS <> 0 BREAK
IF EXISTS(SELECT TOP 1 Udate FROM #MYNAME WHERE Udate < CONVERT(VARCHAR(10),GETDATE(),101))
DROP TABLE #MYNAME
END

Thanks SubqueryCrunch and Sergio Internicola for ur help.#Sergio Internicola, ur logic is right.I think curser take time.I modified SubqueryCrunch's query.It is working fine with me.
DECLARE #SQL NVARCHAR(4000)
SET #SQL = ' '
SELECT
#SQL += 'IF EXISTS(SELECT TOP 1 udate FROM ' + name + ' WHERE DATEDIFF(day,udate,GETDATE()) != 0)
DROP TABLE ' + name +' '
FROM sys.Tables
where name like 'tbl_%'
PRINT #SQL
EXEC sp_executesql #SQL

Related

Create a new column as num containing values of the table name

I have two tables in SQL and want to create a new column from the table name.
In the database, there are many many tables and name are following similar pattern e.g. total_abc_001, total_abc_0002etc... I want only tables starting with total_abc to be selected..
And create a new column as num which contains the values of table name last characters like 001, 002.
I have tried like this:
DROP TABLE IF EXISTS TABLE_NEW_XY
CREATE TABLE TABLE_NEW_XY
(
email VARCHAR(MAX),
Profile VARCHAR(MAX)
)
DECLARE #Sql NVARCHAR(MAX) = '',
#TableName VARCHAR(MAX),
#Id INT
DECLARE Table_Cursor CURSOR FOR
SELECT
ROW_NUMBER() OVER (ORDER BY TABLE_NAME ASC) Id,
TABLE_NAME
FROM
INFORMATION_SCHEMA.TABLES
WHERE
TABLE_TYPE = 'BASE TABLE'
AND TABLE_NAME LIKE 'total_abc_%'
OPEN Table_Cursor
FETCH NEXT FROM Table_Cursor INTO #Id, #TableName
WHILE ##FETCH_STATUS = 0
BEGIN
IF (#Id = 1)
BEGIN
SET #Sql = #Sql + 'SELECT email, Profile FROM '+#TableName
SELECT #SQL
END
ELSE
BEGIN
SET #Sql = #Sql + ' UNION ALL SELECT email, Profile FROM '+#TableName --Modify the columns based on your column names
END
FETCH NEXT FROM Table_Cursor INTO #Id,#TableName
END
CLOSE Table_Cursor
DEALLOCATE Table_Cursor
INSERT INTO TABLE_NEW_XY
EXEC (#Sql)
Now i wanted to add a new column "num in the dynamic sql itself to get the values of the filename in the column as 001,002,003 and so on.
Can you please suggest how to achieve this?
If the number is always 3 digits on the end of the #TableName you can let your dynamic sql hardcode it:
IF (#Id = 1)
BEGIN
SET #Sql = #Sql + 'SELECT email, Profile, ''' + RIGHT(#TableName,3) + ''' tname FROM '+#TableName
SELECT #SQL
END
ELSE
BEGIN
SET #Sql = #Sql + ' UNION ALL SELECT email, Profile, ''' + RIGHT(#TableName,3) + ''' tname FROM '+#TableName --Modify the columns based on your column names
END
This should output:
SELECT email, Profile, '001' tname FROM total_abc_001
UNION ALL SELECT email, Profile, '002' tname FROM total_abc_002
If the n last characters you want to capture are variable you can use the same principle with some more complex find/replace functions

How can I search multiple fields and count nulls for all?

Is there an easy way to count nulls in all fields in a table without writing 40+ very similar, but slightly different, queries? I would think there is some kind of statistics maintained for all tables, and this may be the easiest way to go with it, but I don't know for sure. Thoughts, anyone? Thanks!!
BTW, I am using SQL Server 2008.
Not sure if you consider this simple or not, but this will total the NULLs by column in a table.
DECLARE #table sysname;
SET #table = 'MyTable'; --replace this with your table name
DECLARE #colname sysname;
DECLARE #sql NVARCHAR(MAX);
DECLARE COLS CURSOR FOR
SELECT c.name
FROM sys.tables t
INNER JOIN sys.columns c ON t.object_id = c.object_id
WHERE t.name = #table;
SET #sql = 'SELECT ';
OPEN COLS;
FETCH NEXT FROM COLS INTO #colname;
WHILE ##FETCH_STATUS = 0
BEGIN
SET #sql = #sql + 'COUNT(CASE WHEN ' + #colname + ' IS NULL THEN 1 END) AS ' + #colname + '_NULLS,'
FETCH NEXT FROM COLS INTO #colname;
END;
CLOSE COLS;
DEALLOCATE COLS;
SET #sql = LEFT(#sql,LEN(#sql) - 1) --trim tailing ,
SET #sql = #sql + ' FROM ' + #table;
EXEC sp_executesql #sql;
SELECT COUNT( CASE WHEN field01 IS NULL THEN 1 END) +
COUNT( CASE WHEN field02 IS NULL THEN 1 END) +
...
COUNT( CASE WHEN field40 IS NULL THEN 1 END) as total_nulls
This answer will return a table containing the name of each column of a specified table. (#tab is the name of the table you're trying to count NULLs in.)
You can loop through the column names, count NULLs in each column, and add the result to a total running count.

SQL: Looping through a column, stored the value as a variable, run SQL, then move on to the next line?

I'm currently shifting roles at my job and trying to teach myself some SQL Skills.
Scenario: I'm in charge of 1 database - 10 tables with 10 Primary Keys. Every month, our code team publishes updates to the tables. I am suppose to drop the tables and generate scripts to create the updated tables.
Rather than just drop the old tables and stored procedures, I want to rename my current tables to preserve the structure/data for whatever reason.
In my database, I have an additional table called "TableUpdateList" with 1 column "TableName" and 10 rows - each row containing the name of the updated column (Row 1 = TableName1, Row 2 = TableName2, Row 3 = TableName3)
I would like to be able to "loop" through the TableUpdateList Table and insert each value into a set of SQL statements.
For Example, here are the SQL statements I want to run:
--drop the previous backup table
IF EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES where TABLE_NAME = '*TableName1*'+'_Old') DROP TABLE TableName1_Old
-- rename the current tables to _old
EXEC sp_rename *TableName1*, TableName1_Old;
I'm trying to find a way to scroll through the column of my TableUpdateList and run the above two statements filling in where I've italicized with whatever value is present in that row.
Just taking a wild stab because I think in order to get an answer here, you have to try something so here is my pseudo-code:
Declare #TableNames as List
For i in #TableNames
IF EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES where TABLE_NAME = '*i*'+'_Old') DROP TABLE TableName1_Old
-- rename the current tables to _old
EXEC sp_rename *i*, TableName1_Old;
Oi, thanks in advance for any help or a point in the right direction to where I could do some further reading about the above online.
You can use sp_executesql with CURSORS for such type of work. Here is what i think you need:
Test objects:
CREATE TABLE TableName1 ( ID INT )
GO
CREATE TABLE TableName2 ( ID INT )
GO
CREATE TABLE TableNames ( Name NVARCHAR(MAX) )
GO
INSERT INTO TableNames
VALUES ( 'TableName1' ),
( 'TableName2' )
Script itself:
DECLARE #name NVARCHAR(MAX) ,
#dropStatement NVARCHAR(MAX),
#renameStatement NVARCHAR(MAX)
DECLARE cur CURSOR FAST_FORWARD READ_ONLY
FOR
SELECT Name
FROM dbo.TableNames
OPEN cur
FETCH NEXT FROM cur INTO #name
WHILE ##FETCH_STATUS = 0
BEGIN
IF EXISTS ( SELECT *
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_NAME = #name + '_Old' )
BEGIN
SET #dropStatement = 'DROP TABLE ' + #name + '_Old'
EXEC sp_executesql #dropStatement
END
SET #renameStatement = 'sp_rename ' + #name + ', ' + #name + '_Old';
EXEC sp_executesql #renameStatement
FETCH NEXT FROM cur INTO #name
END
CLOSE cur
DEALLOCATE cur
After this you should add TableName1 and TableName2 again.
Cursors must be avoided as long as possible.
--Preparing script which would check if the old tables exists. If it does,
--it drops the old table
--e.g. first the value 'Table1' is found in TableUpdateList table.
--Then, Table1_Old is deleted and Table1 is renamed to Table1_Old
SELECT 'DROP TABLE ' + b.name + '_Old; EXEC sp_rename ''' + b.name+ ''', ''' + b.name+ '_Old;''' AS [Action]
INTO #Action
FROM INFORMATION_SCHEMA.TABLES A JOIN TableUpdateList B ON A.TABLE_NAME = b.NAME + '_Old'
DECLARE #sql VARCHAR(8000)
SELECT #sql = COALESCE(#sql + ' ', '') + [Action]
FROM #Action
select #sql
--EXEC (#sql)
First verify the value of variable #sql. Then, uncomment the last line to execute the code.
SQL fiddle

Looping through a column in SQL table that contains names of other tables

I have fairly new to using SQL, currently I have a table that has a column that contains the names of all the tables I want to use for one query, so what I want to do is to loop through that column and go to every single one of these tables and then search one of their columns for a value (there could be multiple values), so whenever a table contains the value, I will list the name of the table. Could someone give me a hint of how this is done? Is cursor needed for this?
I don't have enough reputation to comment but is the table with the column that contain the table names all in one column, meaning that all the table names are comma separated or marked with some sort of separator? This would cause the query to be a little more complicated as you would have to take care of that before you start looping through your table.
However, this would require a cursor, as well as some dynamic sql.
I will give a basic example of how you can go about this.
declare #value varchar(50)
declare #tableName varchar(50)
declare #sqlstring nvarchar(100)
set #value = 'whateveryouwant'
declare #getTableName = cursor for
select tableName from TablewithTableNames
OPEN #getTableName
fetch NEXT
from #getTableName into #tableName
while ##FETCH_STATUS = 0
BEGIN
set #sqlstring = 'Select Count(*) from ' + #tableName + 'where ColumnNameYouwant = ' + #value
exec #sqlstring
If ##ROWcount > 0
insert into #temptable values (#tableName)
fetch next
from #getTableName into #tableName
END
select * from #temptable
drop table #temptable
close #getTableName
deallocate #getTableName
I'm currently not able to test this out as for time constraint reasons, but this is how I would go about doing this.
You could try something like this:
--Generate dynamic SQL
DECLARE #TablesToSearch TABLE (
TableName VARCHAR(50));
INSERT INTO #TablesToSearch VALUES ('invoiceTbl');
DECLARE #SQL TABLE (
RowNum INT,
SQLText VARCHAR(500));
INSERT INTO
#SQL
SELECT
ROW_NUMBER() OVER (ORDER BY ts.TableName) AS RowNum,
'SELECT * FROM ' + ts.TableName + ' WHERE ' + c.name + ' = 1;'
FROM
#TablesToSearch ts
INNER JOIN sys.tables t ON t.name = ts.TableName
INNER JOIN sys.columns c ON c.object_id = t.object_id;
--Now run the queries
DECLARE #Count INT;
SELECT #Count = COUNT(*) FROM #SQL;
WHILE #Count > 0
BEGIN
DECLARE #RowNum INT;
DECLARE #SQLText VARCHAR(500);
SELECT TOP 1 #RowNum = RowNum, #SQLText = SQLText FROM #SQL;
EXEC (#SQLText);
DELETE FROM #SQL WHERE RowNum = #RowNum;
SELECT #Count = COUNT(*) FROM #SQL;
END;
You would need to change the "1" I am using as an example to the value you are looking for and probably add a CONVERT/ CAST to make sure the column is the right data type?
You actually said that you wanted the name of the table, so you would need to change the SQL to:
'SELECT ''' + ts.TableName + ''' FROM ' + ts.TableName + ' WHERE ' + c.name + ' = 1;'
Another thought, it would probably be best to insert the results from this into a temporary table so you can dump out the results in one go at the end?

I have the same column in multiple tables, and want to update that column in all tables to a specific value. How can I do this?

The column is "CreatedDateTime" which is pretty self-explanatory. It tracks whatever time the record was commited. I need to update this value in over 100 tables and would rather have a cool SQL trick to do it in a couple lines rather than copy pasting 100 lines with the only difference being the table name.
Any help would be appreciated, having a hard time finding anything on updating columns across tables (which is weird and probably bad practice anyways, and I'm sorry for that).
Thanks!
EDIT: This post showed me how to get all the tables that have the column
I want to show all tables that have specified column name
if that's any help. It's a start for me anyways.
If that's a one time task, just run this query, copy & paste the result to query window and run it
Select 'UPDATE ' + TABLE_NAME + ' SET CreatedDateTime = ''<<New Value>>'' '
From INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME = 'CreatedDateTime'
You could try using a cursor : like this
declare cur cursor for Select Table_Name From INFORMATION_SCHEMA.COLUMNS Where column_name = 'CreatedDateTime'
declare #tablename nvarchar(max)
declare #sqlstring nvarchar(max)
open cur
fetch next from cur into #tablename
while ##fetch_status=0
begin
--print #tablename
set #sqlstring = 'update ' + #tablename + ' set CreatedDateTime = getdate()'
exec sp_executesql #sqlstring
fetch next from cur into #tablename
end
close cur
deallocate cur
You can use the Information_Schema.Columns to build update scripts for you.
Declare #ColName as nVarchar(100), #NewValue as nVarchar(50)
Set #ColName = 'Modified' -- 'your col name'
Set #NewValue = '2013-11-04 15:22:31' -- your date time value
Select 'Update ' + TABLE_NAME + ' set ' + COLUMN_NAME + ' = ''' + #NewValue + '''' From INFORMATION_SCHEMA.COLUMNS Where column_name = 'modified'