**除了一个值,我需要Select Query [英] ** I need Select Query except one value

查看:72
本文介绍了**除了一个值,我需要Select Query的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨朋友们,



我的桌子上有60列



i需要全部选择值除了一个值。



 选择 * 来自 mytable将选择 所有  60 列。





但我只需要59列。例如,我想忽略第60列值。



所以我需要写一个像


$ b $的查询b

 选择 column1,column2 .... column59 来自 mytable 

还是其他一些简单的方法?



Plz告诉你的建议朋友..

解决方案

您需要编写类似的查询,从mytable 中选择column1,column2 .... column59。



始终在查询中命名列是最佳做法。这有助于添加新列或删除旧列。



使用 SELECT * ,将检索最近添加到表中的新列,并且程序可能不是能够正确处理它。



使用 SELECT * ,如果从表中删除一列,应用程序可能会以奇怪的方式出错需要调试。当使用 SELECT column1,column2,column3 格式时,SQL查询将失败并显示缺少列错误。


Hi ... < br $>


查看以下示例查询。



   -   表创建 
创建 TABLE 测试(Col1 INT ,Col2 INT ,Col3 INT ,Col4 INT ,Col5 INT
- 本地变量声明
DECLARE @ TableName VARCHAR 100 ), @ SqlQuery NVARCHAR ( 1000 ), @ ExcludeColPos INT

- 分配默认值
SELECT @ TableName = ' 测试',@ ExcludeColPos = 2 - 此处使用1到5

- 插入样本数据
INSERT INTO 测试(Col1,Col2,Col3,Col4,Col5)
SELECT 1 2 3 4 5
UNION 全部
SELECT
6 7 8 9 10
UNION ALL
SELECT 11 12 13 14 15

- < span class =code-comment>合并排除列以外的所有列
SELECT @ SqlQuery = COALESCE (@ SqlQuery + ' ,' ' ')+ Column_Name FROM INFORMATION_SCHEMA.COLUM NS WHERE Table_Name = @ TableName AND Ordinal_position<> @ ExcludeColPos

- 框架SQL选择语句
SELECT @SqlQuery = ' SELECT' + @ SqlQuery + ' FROM' + @ TableName
SELECT @SqlQuery = @ SqlQuery + ' SELECT COUNT(1)FROM' + @ TableName

PRINT @ SqlQuery

- 执行查询
EXECUTE sp_executesql @ SqlQuery

- 删除示例表
DROP 测试



问候,

GVPrabu






更好的选择传统方式的列,如选择col1,col2,....来自表格

(因为它会比下面的SP更快)



你可以尝试跟随sp,





创建PROCEDURE [dbo]。[TEST] 

@Schema varchar(10),
@Table varchar(20),
@ColumnExclude Varchar(20),
@condition Varchar(max)
AS
开始

声明@column varchar(max)
声明@ query varchar(Max)

set @ column =(SELECT COLUMN_NAME +','
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = @Table AND TABLE_SCHEMA = @ Schema and COLUMN_NAME!= @ColumnExclude
FOR XML PATH(''))

set @ column = LEFT(@column, LEN(@column)-1)

设置@query ='select'+ @ column +'来自'+ @ Schema +'。'+ @ Table +'(nolock)'+'where'+ @ condition
exec(@query)

END







和然后使用它像



执行测试'Schemaname','TableName','ColumnName_toexclude','condition'



例如

 exec test'dbo','emp','salary','name =''john'''





希望它会有所帮助...



快乐编码.. :) / BLOCKQUOTE>

Hi friends,

i've 60 columns in my table

i need to select all values except one value.

select * from mytable  will select all 60 columns.



But i need 59 columns only. for example i want to neglect the 60th column value.

so i need to write a query like

select column1,column2....column59 from mytable

or some other easy way is there??

Plz tell ur suggestion Friends..

解决方案

You need to write query like select column1,column2....column59 from mytable.

It is a best practice to always name the columns in a query. This helps when new columns are added or old columns are deleted.

With SELECT *, a new column recently added to the table would be retrieved and the program may not be able to handle it properly.

With SELECT *, if a column is deleted from the table, the application may fault in strange ways and require debugging. When SELECT column1,column2,column3 format is used the SQL Query will fail with a missing column error.


Hi...

Check the below sample Query.

-- Table Creation
CREATE TABLE Test(Col1 INT,Col2 INT,Col3 INT,Col4 INT,Col5 INT)
-- Local variable Declaration
DECLARE @TableName VARCHAR(100), @SqlQuery NVARCHAR(1000), @ExcludeColPos INT

-- Assign default values
SELECT @TableName='Test',	@ExcludeColPos=2 -- Here use 1 to 5

-- Insert Sample data
INSERT INTO Test (Col1,Col2,Col3,Col4,Col5)
SELECT 1,2,3,4,5,
UNION ALL
SELECT 6,7,8,9,10
UNION ALL 
SELECT 11,12,13,14,15

-- Combine all columns except exclude column
SELECT @SqlQuery = COALESCE(@SqlQuery+',','')+Column_Name FROM INFORMATION_SCHEMA.COLUMNS WHERE Table_Name=@TableName AND Ordinal_position <> @ExcludeColPos

-- Frame SQL Select Statement
SELECT @SqlQuery='SELECT ' + @SqlQuery +' FROM '+ @TableName
SELECT @SqlQuery=@SqlQuery+' SELECT COUNT(1) FROM '+ @TableName

PRINT @SqlQuery

-- Execute the Query 
EXECUTE sp_executesql @SqlQuery

-- Drop sample table
DROP TABLE Test


Regards,
GVPrabu


Hi,

better you select columns in the traditional way like select col1,col2,.... from table
(as it will be faster then the SP below)
or
you can try following sp,


Create PROCEDURE [dbo].[TEST]   
 
 @Schema varchar(10),
 @Table varchar(20),   
 @ColumnExclude Varchar(20),
 @condition Varchar(max)
AS  
Begin

declare @column varchar(max)
declare @query varchar(Max)

set @column=(SELECT COLUMN_NAME +',' 
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = @Table AND TABLE_SCHEMA=@Schema and COLUMN_NAME !=@ColumnExclude 
FOR XML PATH(''))

set @column= LEFT(@column,LEN(@column)-1)

set @query = 'select '+@column+' from ' +@Schema+'.'+@Table+'(nolock)'+'where '+@condition
exec( @query)

END




and then use it like

exec test 'Schemaname','TableName','ColumnName_toexclude','condition'


for example

exec test 'dbo','emp','salary','name=''john'''



hope it will help...

Happy coding.. :)


这篇关于**除了一个值,我需要Select Query的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆