从数据库只返回特定的列? [英] returning only particular column from database??

查看:98
本文介绍了从数据库只返回特定的列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在数据库表中存储了10个产品ID,该产品ID中没有其他列..但是我只想从数据库中检索ID列,并希望更新仍为空的ID?

例如,如果每当我更新新产品时,某些数据存储在1,2,3,4,6,8,10 id中,我希望将其自动存储在第5个id,下一个存储在第7个id ..

I have 10 product id''s stored in my database table which has few other columns..but i want to retrieve only the id column from the database and want to update the id which are still empty??

for example if some data is stored in 1,2,3,4,6,8,10 id''s whenever i update a new product i want it to be automatically stored at 5th id and next one at 7th id..
is it possible?

推荐答案

如果列ID是标识类型,则无法指定您的ID,如果没有,则可以编写一个返回id列中最小的丢失ID.

您可以使用以下查询从表中获取最小的缺失ID.

If the column id is of identity type then its not possible to specify your id, in case its not then you can write a function which returns the smallest missing id from the id column.

You can use following query to get the smallest missing id from you table.

Select Top 1 ID + 1 from YourTable T1
where not exists 
   (select * from YourTable T2 where T2.ID = T1.ID + 1)


嘿,

有什么理由使用这种方法吗?

因为这对我来说可能有危险.如果表未正确关联,则可能导致错误的引用.除此之外,新行将始终添加到底部(据我所知至少!)

这完全取决于ID是否为AutoNumber.
Hey,

Is there any reason for this approach?

Because it sounds potentially dangerous to me. It could lead to false references if tables are not properly related. Besides that, new rows will always be added to the bottom (as far as i know atleast!)

It all depends on the ID being an AutoNumber or not.


我已经更改了应用程序,以便可以轻松完成...现在我只希望生成ID号,无论哪个在10中为空,并在不添加任何新行的情况下在偏移量列中显示并存储该ID ...是否有可能?
i have changed the application so that it can be easily done...now i only want the id number to be generated whichever is empty in 10 and display and store that id in a offset column without adding any new row...is it possible??


这篇关于从数据库只返回特定的列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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