Monday, April 26, 2010

Print prime numbers

This logic finds all the prime numbers upto the number specified.

flag=1
primeNo=inputbox("Enter a number")
For i=2 to primeNo
flag=1
For j=2 to i/2
If i mod j=0 Then
flag=0
End If
Next

If flag=1 Then
msgbox i
End If
Next

Wednesday, April 14, 2010

Algorithm to check palindrome

Word="level"
lengthVar = Len(Word)
For i = 1 To lengthVar
v1 = v1 + Mid(Word, i, 1)
Next
For i = lengthVar To 1 Step -1
v2 = v2 + Mid(Word, i, 1)
Next
If v1 = v2 Then
MsgBox "given word is palindrome"
Else
MsgBox "given word is not palindrome "
End If

Monday, April 12, 2010

How to access Database using QTP

Method 1:
Option explicit
Dim connectionObj,recordsetObj,commandObj
Dim connectionStr,queryStr

Set connectionObj=createobject("ADODB.connection")
Set commandObj=createobject("ADODB.command")
Set recordsetObj=createobject("ADODB.recordset")
connectionStr="driver=sql server;server=xyz;database=test;uid=sa;pwd=sa"
queryStr="insert into studentdetails (stdno,stdname,age,groupname,sex,dateofbirth) values (6,'xyz',22,'ijk','M','08/18/80')"


connectionObj.open connectionStr
set commandObj.activeconnection=connectionObj
commandObj.commandtext=queryStr
Set recordsetObj=commandObj.execute
connectionObj.close
'commandObj.close
recordsetObj.close


If you have set of records in result set and if you want to travese through each of these then use below code:

while not recordsetObj.EOF
noRecords=recordsetObj.fields.count 'This finds the no. of columns in result set
var1=recordsetObj("fieldname1").value 'This retrieve the columnname values
var2=recordsetObj("fieldname2").value
recordsetObj.movenext
wend



Method 2
Option explicit
Dim connectionObj,recordsetObj,commandObj
Dim connectionStr,queryStr
Set commandObj=createobject("ADODB.command")
Set recordsetObj=createobject("ADODB.recordset")
connectionStr="driver=sql server;server=xyz;database=test;uid=sa;pwd=sa"
queryStr="insert into studentdetails (stdno,stdname,age,groupname,sex,dateofbirth) values (6,'xyz',22,'ijk','M','08/18/80')"
commandObj.activeconnection=connectionStr
commandObj.commandtext=queryStr
Set recordsetObj=commandObj.execute
'connectionObj.close
'commandObj.close
'recordsetObj.close

Write a query to find the Nth largest salary of an employee

Select * From Employee E1 Where
(N-1) = (Select Count(Distinct(E2.Salary)) From Employee E2 Where
E2.Salary > E1.Salary)

for ex: for 3rd largest salary of an employee, the query is:
Select * From Employee E1 Where
2 = (Select Count(Distinct(E2.Salary)) From Employee E2 Where
E2.Salary > E1.Salary)