开发者

How to declare variables in select statement

开发者 https://www.devze.com 2023-02-21 21:45 出处:网络
Please look at this query. select name form table Let suppose this returns 100 name records. I want to store each record in variable and then use this variable in another select

Please look at this query.

select name form table

Let suppose this returns 100 name records.

I want to store each record in variable and then use this variable in another select

statement.Perhaps I have to do it with inner select but I do not know how.

How this is possible ?

I am working on three complex queries with complex joins I use the code of product and pass them to queries .If first query return the result fine else I move to second query else to third on.I have done this in c# very easily using loops but 开发者_运维知识库it is taking very much time(About 5 minutes for 7000 records) Now I shall store the code in variable and pass it to first query in SQL if count of it result is greater than 0 fine else I move to 2nd else to third one. And so I shall iterate to all productCode Records. `select * from productsale where productname like' %';


Is the a reason you're not using JOIN's? That is usually the best way to use a value in one table to extract values from other tables.

Something like

   select name , dept from name_table nt, dept_table dt
   where nt.name = dt.name

Otherwise you're looking to use cursors. There are a lot of good tutorials on the web, and you may want to find a tutorial specific the DB version you are using.

I hope this helps.

0

精彩评论

暂无评论...
验证码 换一张
取 消