473,388 Members | 1,574 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

SQL Query Error Help

1
When running the below SQL Query I keep getting the following error:
Server: Msg 4924, Level 16, State 1, Line 1
ALTER TABLE DROP COLUMN failed because column 'ContractDef' does not exist in table 'zContractDefault'.

For some reason it is only returning the first 11 chars of the column name?

Any help would be greatly appreciated...

This query searches a DB and determines which columns are 'Invalid' if the column name is >30 and contains '_T' and then removes them and gives a report:

USE PM7Database


GO
--NOTE: Display to user a list of bad columns in the result set.
Select c.table_name, c.column_name, charindex('_T', c.column_name) as Char_Index, substring(c.column_name, charindex('_T', c.column_name), 100) as Bad_Col_Portion, len(substring(c.column_name, charindex('_T', c.column_name), 100)) as Bad_Col_Port_Length
from information_schema.columns as c
Inner Join
information_schema.tables as t
ON
c.table_name = t.table_name
where t.table_type <> 'VIEW'
and
len(substring(c.column_name, charindex('_T', c.column_name), 100)) > 30
and
charindex('_T', c.column_name) <> 0
order by 1, 2

--NOTE: Display a count of all columns in the database.
-- Used to help validate that only appropriate fields were removed.
Select 'Column Count of all Columns in the Database: ' + Convert(Varchar(50), Count(*)) as 'Comment.......................................... ..........Comment'
from information_schema.columns




-- SCRIPT SETUP!

Declare @Specific_Col varchar(100)
Declare @Specific_Table varchar(100)
Declare @Spec_Col_Eval varchar(100)
Declare @Char_Index_Eval int
Declare @Spec_Tbl_Eval varchar(100)
Declare @DropStmt varchar(2000)
Declare @RowCount int
Declare @RowIncrement int
set @RowIncrement = 0

-- Table Variable setup to capture some of the process that goes on with this script.
Declare @TableResults Table
(
DropStmt varchar(2000),
DropStmtLength int
)


-- Worker Table Variable used to capture from the database all
Declare @TableInspect Table
(
[Database] varchar(70),
Table_name varchar(70),
Column_name varchar(100),
Char_Index int
)

Insert into @TableInspect
Select c.table_catalog, c.table_name, c.column_name, charindex('_T', c.column_name)
from information_schema.columns as c
Inner Join
information_schema.tables as t
ON
c.table_name = t.table_name
where t.table_type <> 'VIEW'
and
len(substring(c.column_name, charindex('_T', c.column_name), 100)) > 30
and
charindex('_T', c.column_name) <> 0
order by 1 desc, 2


-- Set a counter for # of columns to drop.
-- Used for reporting AND required to make sure the last columns are actually dropped.
Select @RowCount = Count(*) from @TableInspect as [Comment........................................... .........Comment]
-- Lets the user know how many columns are actually found and need to be dropped.
-- If UDF Fields are present, they will not be dropped but will be added to the counter.
Select 'Total Columns to Drop: ' + Convert(Varchar(100), @RowCount) as [Comment........................................... .........Comment]


-- Cursor used to evaluate column names as completely valude to drop and set
-- up a drop statement to remove the columns. Pulls data from the Table Variable
-- that was declared above.
declare ColumnSearcher cursor for
Select Table_Name, Column_Name, Char_Index
from @TableInspect

set nocount on

open ColumnSearcher
-- These 3 variables used by the fetch are required in the evaluations below.
fetch next from ColumnSearcher into @Specific_Table, @Specific_Col, @Char_Index_Eval
while @@fetch_status = 0


-- Begin Main search for erroneous columns.
Begin

-- This conditional statement ensures that that the Drop statement is prepared to be run.
-- Requirements for Drop Statement to run are:
-- Req: The Currently Fetched table must be a different name than the last fetched table.
-- Reason: Allows less actual hits to the database for columns to be dropped. Each Drop Statement
-- can contain 2000 characters. This allows for roughly 40 columns to be dropped from a single table.
-- That estimate is based on an average Column size using the following equation:
-- 15 (high avg. base column name size) + 34 (constant size of the erroneous column) = 49
-- 2000/49 = 40.8... round down to 40.
-- This is a limitation.
-- Req: The Last Fetched Table and the Currently Fetched table cannot be Null. If so then it is likely
-- the first row in the fetch. This is wehre @Spec_Tbl_Eval gets first assigned. By a null value of
-- the first row.

If @Spec_Tbl_Eval <> @Specific_Table and @Spec_Tbl_Eval IS NOT NULL and @Specific_Table IS NOT NULL
Begin
Set @DropStmt = 'Use ' + db_name() + ' ' + @DropStmt

-- Log into the @TableResults table variable the results of what the drop statement actually is dropping.
Insert @TableResults
Values (@DropStmt, len(@dropstmt))

exec(@DropStmt)

-- Force Drop Statement to be NULL every time it is executed. This allows a change to a different table
-- for every drop that is submitted to SQL Server.
Set @DropStmt = NULL
Set @Spec_Tbl_Eval = @Specific_Table
End
else
Set @Spec_Tbl_Eval = @Specific_Table


-- This conditional statement is used to build the complete drop statement.
If @Char_Index_Eval <> 0 and @Char_Index_Eval IS NOT NULL
Begin

-- If the @DropStmt variable is null, then a beginning to the drop statement must first be added.
if @DropStmt IS Null
Begin

-- This block sets the beginning of the drop statement and evalutes the @Specific_Col which has been
-- deemed droppable to make sure it is not a UDF field.
if substring(@Specific_Col, 1, 4) <> 'udf_'
Begin
Set @DropStmt = 'Alter Table ' + @Specific_Table + ' Drop Column ' + @Specific_Col
Set @RowIncrement = @RowIncrement + 1
End
Else

-- Forces notification regarding UDF fields.
Insert @TableResults
Values ('UDF COLUMN SKIPPED - ' + @Specific_Col, 0)
Set @RowIncrement = @RowIncrement + 1

End
Else
Begin

-- This block adds onto an NOT NULL @DropStmt variable extra columns that need to be dropped from a specific
-- table.
Set @DropStmt = @DropStmt + ', ' + @Specific_Col
Set @RowIncrement = @RowIncrement + 1
End

End

-- Check for last drop statement and make sure it is executed.
If @RowIncrement = @RowCount
Begin
Set @DropStmt = 'Use ' + db_name() + ' ' + @DropStmt
exec(@DropStmt)
Set @DropStmt = NULL
Set @Spec_Tbl_Eval = NULL
End

fetch next from ColumnSearcher into @Specific_Table, @Specific_Col, @Char_Index_Eval
End

close ColumnSearcher
deallocate ColumnSearcher

-- Display the results of the @TableResults Table to the customer. Usefull for troubleshooting missing columns.
Select * from @TableResults

-- Final count of all columns with in the database. Note that UDF Fields have not been dropped so it may
-- appear that you have additional columns that still remain. Make sure to subtract from this number any
-- left over UDF Columns which you can find by looking at the @TableResults.
Select 'Total Columns Remaining: ' + Convert(Varchar(100), Count(*)) as 'Comment.......................................... ..........Comment'
from information_schema.columns
May 17 '07 #1
0 4110

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: Polar | last post by:
I am having troubles finding the parse error in this script. I've been checking for weeks. I am too new to the subject I guess. I am trying to show a readord and them have a form at the bottom...
6
by: Martin Lacoste | last post by:
Ok, before I headbutt the computer... don't know why when I add criteria in a query, I get an 'invalid procedure call'. I also don't know why after searching the help in access, the various access...
4
by: Alan Lane | last post by:
Hello world: I'm including both code and examples of query output. I appologize if that makes this message longer than it should be. Anyway, I need to change the query below into a pivot table...
3
by: ILCSP | last post by:
Hello, I'm fairly new to the concept of running action pass through queries (insert, update, etc.) from Access 2000. I have a SQL Server 2000 database and I'm using a Access 2K database as my...
36
by: Liam.M | last post by:
hey guys, I have one last problem to fix, and then my database is essentially done...I would therefore very much appreciate any assistance anyone would be able to provide me with. Currently I...
5
by: elitecodex | last post by:
Hey everyone. I have this query select * from `TableName` where `SomeIDField` 0 I can open a mysql command prompt and execute this command with no issues. However, Im trying to issue the...
9
by: Kelii | last post by:
I've been trying to get this piece to work for a few hours, but have given up. I hope someone out there can help, I think the issue is relatively straightforward, but being a novice, I'm stumped....
10
by: aaronrm | last post by:
I have a real simple cross-tab query that I am trying to sum on as the action but I am getting the "data type mismatch criteria expression" error. About three queries up the food chain from this...
4
by: franc sutherland | last post by:
Hello, I am using Access 2003. I am having trouble trapping the "can't append all the records in the append query" error message when appending data to a query from a table which is linked to...
9
by: dizzydangler | last post by:
Hi, all! After receiving a lot of help from ADezzi, Msquared, and NeoPa getting my ms access 2007 db functioning, I've run into another hitch that I haven't been able to solve. The database itself...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.