الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

كيفية استبدال حقل (field) من جدول بحقل جدول اخر

بدأه ababil في 2 يوليو 2012 · 6 رد · 2,563 مشاهدة · في قواعد بيانات Visual FoxPro
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

السلام عليكم اخوان

ارجوا من الاخوة الافاضل والاخ اسد العراق ان يكتبوا كود استبدال حقل (field) من جدول بحقل جدول اخر واتمنى استعمال اكثر من طريقة

مثل استعمال REPLACE مع DO WHILE ENDDO و SCAN ENDSCAN

او طرق اخرى تراها مناسبة للفائدة

وايضا كود استبدال عدة صفوف (array) بصفوف من جدول اخر بشرط او بدون شرط

للتوضيح

لنفرض اسم الجدول الاول table1 واسم الجدول الثاني table2

فيهما حقول بأسم FieldName1 و FieldName2

فكيف سيكون شكل الاكواد

وهل يمكن استخدام REPLACE ALL في مثل هذه الحالة

شخصيا جربت هذا الكود

REPLACE ALL table1.FieldName1 WITH table1.FieldName2

فقام باستبدال كل الحقول من الجدول الاول بأول حقل من الجدول الثاني

والمراد ان يستبدل كل حقول الجدول الاول بكل حقول الجدول الثاني

ارجوا ان لا اكون اطلت عليكم

مع الشكر

#2

جرب هذا الكود علي اعتبار ان كلا الجدولين يحتويان علي بيانات و علي عدد متساوي من السجلات

select 2

use table2 in 2

select 1

use table1 in 1

DO WHILE .NOT. EOF()

mFieldName1 = FieldName1

SELECT 2

REPLACE FieldName2 WITH mFieldName1

skip

select 1

skip

loop

enddo

1

Persistent Fox

#3

السلام عليكم اخي العزيز يجب مراعاه قبل الاستبدال السهل والسريع ان تكون الحقول متساوية في النوع والحجم او يكون حجم الاول اكبر من الحجم المراد جلب البيانات منه اما عن استخدام replace فهي تفيد للكل او عدد معين من السجلات بقيمة محددة ويمكن اضافة الشرط لها في حالتها اما اذا اردنا استخدامها بفكرتك او نسخ عمود كامل الى عمود اخر اي تجعلهُ توام نفس طريقة الاخ xcode باستخدام replace مع داله اللوب الغير معروف عددها وهي do while اما اذا كنت تعرف عدد السجلات للاستبدال فيمكن استخدام الدالة for اليك المثال في فكرتك وانتبة استخدام for بعدد معين واستخدام do في الاستبدال لعدم معرفة عدد السجلات لنفترض المثال

SET SAFETY OFF
CREATE TABLE lion(name c(4),old n(2),note c(5))
FOR i=1 TO 50 
INSERT INTO lion VALUES ("lio"+TRANSFORM(i),i,"")
NEXT 
GO top
BROWSE NOWAIT 
CREATE TABLE lion1(name c(4),old n(2),note c(5))
FOR i=1 TO 50 
INSERT INTO lion1 VALUES ("",i,"lolo")
NEXT 
GO TOP
BROWSE NOWAIT 

SELECT lion1
DO WHILE !EOF()
REPLACE name WITH lion.name
SELECT lion
skip
SELECT lion1
skip
ENDDO 

SELECT lion1
GO TOP
BROWSE LAST

SELECT lion 
GO TOP 
BROWSE LAST

اخي الكود اعلاه فكرتة الاتي جدول ليون تم اضافة بيانات للاسم فقط اما جدول ليون1 تم اضافة بيانات للحقول فقط ثم قمنا باستبدال حقل الاسم في ليون الى حقل الاسم في ليون1 انتظر اي سوال منك تحياتي للجميع

replace.zip

تم تعديل هذه المشاركة بواسطة lion iraq في 2 يوليو 2012 في 09:09 — السبب: توضيح الكود

1
#4

مشكور اخي xcode على الكود الجميل

مع اني لم افهم لماذا استخدمت mFieldName1 = FieldName1

اقصد لما لا اكتب الاسم الاصلي مباشرة بدون اسناده الى متغير ويكون كذا

REPLACE FieldName2 WITH FieldName1

ربما كنت تقصد ان mFieldName1 هو حقل للجدول المراد اخذ القيم منه

لاني سبق ان ذكرت ان الحقول اسمائها FieldName1 و FieldName2

اشكرك مرة اخرى لتخصيصك وقت للرد علي

بالنسبة الى الاخ اسد العراق بعد التحية

استفدت من الكود الذي كتبته اشياء جميلة

واستطيع ان اكتب كود من خلال شرحك وهو ايضا مفيد في الاستبدال

الكود هو

SELECT lion1 
GO TOP
FOR i=1 TO 50
REPLACE lion1.name WITH lion.name 
SELECT lion 
skip 
SELECT lion1 
skip 
NEXT

اما اذا لم اكن اعرف عدد السجلات التي يمكن اضافتها يكون كالتالي

SELECT lion
COUNT TO X1
GO TOP
SELECT lion1 
GO TOP
FOR i=1 TO  X1
REPLACE lion1.name WITH lion.name 
SELECT lion 
skip 
SELECT lion1 
skip 
NEXT  
GO TOP

وهذا يعني ان REPLACE ALL لا يمكن استخدامها في مثل هكذا حالة

اشكرك على الافادة

هل من الممكن اخي اسد اذا سمح لك الوقت ان تكتب مشاركة تشرح فيها كيفية استخدام الدوال التالية مع مثال بسيط

DO WHILE ENDDO

DO CASE ENDCASE

FOR EACH ... ENDFOR Command

FOR ... ENDFOR Command

SCAN ... ENDSCAN Command

لاهمية استخدامتها الكثيرة

وياريت تكون كلها على مثال او جدول واحد

مع الشكر

#5

اخي العزيز اذا كنت تريد امثلة علي هذة الدوال مع شرح رائع و بسيط يمكن ان تجدها في ملف المساعدة الموجود مع VFP بالطبع هذا لا يغني عن شرح و امثلة الاخ اسد العراق الرائعة .

Persistent Fox

#6

اخوتي الاعزاء السلام عليكم

ابابيل انا اشجعك على ابتكار طرق خاصة لك من خلال فهم طريقة وعمل الايعازات والدوال بارك الله فيك وقم بالتفكير وابتكار الطرق من تلقاء نفسك ضمن شروط عملها في الفوكي برو وان تشابه طرقك مع الغير

اما بالنسبة للدوال في الفيجوال فوكس برو هنالك بعض التشابه في عملها ولكن لكل دالة عمل متخصص اليك وظائف الدوال الذي ذكرتهم

foreach : تنفيذ مجموعة من الأوامر لكل عنصر في مجموعة Visual FoxPro خاصة بالتطبيقات والمصفوفات

FORVAR: تنفيذ مجموعة من الأوامر لعدد محدد من المرات. يمكن الاستخدام على الجداول من ضمنها

DOWHILE: تنفيذ مجموعة من الأوامر ضمن حلقة مشروط.يمكن الاستخدام على الجداول من ضمنها

DOCASE: تنفيذ أول مجموعة من الأوامر التي مشروط تقييم التعبير إلى True (ت).يمكن الاستخدام على الجداول من ضمنها

scan: تحريك مؤشر السجل من خلال الجدول المحدد حاليا وتنفيذ مجموعة من الأوامر لكل سجل أن تتوفر فيه الشروط المحددة.يمكن الاستخدام على الجداول من ضمنها

وانا اؤيد الاخ xcode الكتاب التعليمي الالكتروني هو الاساس للفيجوال وانا بالنسبة لي هو المعلم الاول افهم عمل الدالة من خلالة واقوم بدراسة الامثلة التوضيحيه به والباقي علي في استخدامها في ضمن شروط استخدامها

تحياتي للجميع واذا كان مثال لم تفهم معناهخ من الكتاب الالكتروني اذكرة وان شاء الله سوف نتعاون معا نحن اعضاء القسم لايصال الفكرة المناسبة لعمل المثال

#7

السلام عليكم 

ملف يدعم بعض الايعازات 

تحياتي   

 1

FOR ... ENDFOR Command

Executes a set of commands a specified number of times.

Syntax

FOR Var = nInitialValue TO nFinalValue [sTEP nIncrement] Commands [EXIT] [LOOP] ENDFOR | NEXT

Arguments

Var

Specifies a variable or an array element that acts as the counter. The variable or array element doesn't have to exist before FOR ... ENDFOR is executed.

nInitialValue TO nFinalValue

nInitialValue is the initial value of the counter; nFinalValue is the final value of the counter.

STEP nIncrement

nIncrement is the amount the counter is incremented or decremented. If nIncrement is negative, the counter is decremented. If you omit STEP, the counter is incremented by 1.

Commands

Specifies the Visual FoxPro commands to be executed. Commands can include any number of commands.

2

EXIT

Transfers control from within the FOR ... ENDFOR loop to the command immediately following ENDFOR. You can place EXIT anywhere between FOR and ENDFOR.

LOOP

Returns control directly back to the FOR clause without executing the statements between LOOP and ENDFOR. The counter is incremented or decremented as if ENDFOR were reached. LOOP can be placed anywhere between FOR and ENDFOR.

Remarks

A variable or an array element is used as a counter to specify how many times the Visual FoxPro commands inside the FOR ... ENDFOR loop are executed.

The Visual FoxPro commands after FOR are executed until ENDFOR or NEXT is reached.

The counter MemVarName is then incremented by the value of nIncrement. If you omit the STEP clause, the counter is incremented by 1.

The counter is then compared with nFinalValue. If the counter is less than or equal to nFinalValue, the commands following the FOR clause are executed again.

If the counter is greater than nFinalValue, the FOR ... ENDFOR loop is exited and program execution continues with the first command following ENDFOR or NEXT.

Note The values of nInitialValue, nFinalValue, and nIncrement are only read initially.

However, changing the value of the counter MemVarName inside the loop affects the number of times the loop is executed.

3

If the value of nIncrement is negative and the initial value nInitialValue is greater than the final value nFinalValue, the counter is decremented each time through the loop.

Example

FOR ... ENDFOR Command Example

In Example 1, the numbers 1 through 10 are displayed.

Example 2 uses memory variables for the initial, final, and STEP values to display all even-numbered records from 2 through 10 in customer.

* Example 1

CLEAR

FOR gnCount = 1 TO 10

? gnCount

ENDFOR

* Example 2

SET TALK OFF

CLOSE DATABASES

OPEN DATABASE (HOME(2) + 'Data\testdata')

USE customer && Opens Customer table

STORE 2 TO gnI && Initial value

STORE 10 TO gnJ && Final value

STORE 2 TO K && Step value

FOR gnCount = gnI TO gnJ STEP K

GOTO gnCount && Move record pointer

DISPLAY company && Display company name

ENDFOR

4

DO WHILE ... ENDDO Command

Executes a set of commands within a conditional loop.

Syntax

DO WHILE lExpression Commands [LOOP] [EXIT] ENDDO

Arguments

lExpression

Specifies a logical expression whose value determines whether the commands between DO WHILE and ENDDO are executed.

As long as lExpression evaluates to true (.T.), the set of commands are executed.

Commands

Specifies the set of Visual FoxPro commands to be executed as long as lExpression evaluates to true (.T.).

LOOP

Returns program control directly back to DO WHILE.

LOOP can be placed anywhere between DO WHILE and ENDDO.

5

EXIT

Transfers program control from within the DO WHILE loop to the first command following ENDDO.

EXIT can be placed anywhere between DO WHILE and ENDDO.

Remarks

Commands between DO WHILE and ENDDO are executed for as long as the logical expression lExpression remains true (.T.).

Each DO WHILE statement must have a corresponding ENDDO statement.

Comments can be placed after DO WHILE and ENDDO on the same line.

The comments are ignored during program compilation and execution.

6

Example

DO WHILE ... ENDDO Command Example

In the following example, the number of products in stock priced over $20 is totaled in the DO WHILE loop until the end of the file (EOF) is encountered. The DO WHILE loop is exited and the total is displayed.

CLOSE DATABASES

OPEN DATABASE (HOME(2) + 'Data\testdata')

USE products && Opens Products table

SET TALK OFF

gnStockTot = 0

DO WHILE .T. && Begins loop

IF EOF( )

EXIT

ENDIF

IF unit_price < 20

SKIP

LOOP

ENDIF

gnStockTot = gnStockTot + in_stock

SKIP

ENDDO && Ends loop

CLEAR

? 'Total items in stock valued over 20 dollars:'

?? gnStockTot

7

SCAN ... ENDSCAN Command

Moves the record pointer through the currently selected table and executes a block of commands for each record that meets the specified conditions.

Syntax

SCAN [NOOPTIMIZE] [scope] [FOR lExpression1] [WHILE lExpression2] [Commands] [LOOP] [EXIT] ENDSCAN

Arguments

NOOPTIMIZE

Prevents Rushmore optimization of SCAN.

For more information, see SET OPTIMIZE and "Understanding Rushmore Technology" in Chapter 15, Optimizing Applications, in the Programmer's Guide.

Scope

Specifies a range of records to be scanned. Only the records within the range are scanned.

The scope clauses are: ALL, NEXT nRecords, RECORD nRecordNumber, and REST.

بنود نطاق هي: ALL ، NEXT nRecords ، RECORD nRecordNumber ، و REST .

For more information on scope clauses, see the Scope Clauses online topic.

8

The default scope for SCAN is all records (ALL).

FOR lExpression1

Executes commands only for records for which lExpression1 evaluates to true (.T.).

Including the FOR clause lets you filter out records you don't want scanned.

Rushmore optimizes a query created with SCAN ... FOR if lExpression1 is an optimizable expression .

For best performance, use an optimizable expression in the FOR clause.

For more information, see SET OPTIMIZE and "Understanding Rushmore Technology" in Chapter 15, Optimizing Applications, in the Programmer's Guide.

WHILE lExpression2

Specifies a condition whereby the commands are executed for as long as lExpression2 evaluates to true (.T.).

Commands

Specifies the Visual FoxPro commands to be executed.

LOOP

Returns control directly back to SCAN.

LOOP can be placed anywhere between SCAN and ENDSCAN.

9

EXIT

Transfers program control from within the SCAN ... ENDSCAN loop to the first command following ENDSCAN.

EXIT can be placed anywhere between SCAN and ENDSCAN.

ENDSCAN

Indicates the end of the SCAN procedure.

Remarks

SCAN automatically advances the record pointer to the next record that meets the specified conditions and executes the block of commands.

You can place comments after ENDSCAN on the same line.

The comments are ignored during program compilation and execution.

Example

SCAN ... ENDSCAN Command Example

The following example uses a SCAN ... ENDSCAN loop to display all the companies in Sweden.

CLOSE DATABASES

OPEN DATABASE (HOME(2) + 'Data\testdata')

USE customer && Opens Customer table

CLEAR

SCAN FOR UPPER(country) = 'SWEDEN'

? contact, company, city

ENDSCAN

مواضيع مشابهة