how to Auto fill fields in powerbuilder - powerbuilder

i'm new to PowerBuilder 10.5 i'm trying to create a cost application , i want to autocomplete the row when i pick a enter image description herematerial from a DDDW i want to auto fill the price & unit. there are 2 tables 1Material - Unit - Price 2- Itemname DDDW - Quantity - Unit - Price
i did it on MSaccess but i don't know how to code it on PowerBuilder
on access the code was
Private Sub ItemName_AfterUpdate()
Unit = ItemName. Column(2)
Price = ItemName. Column(3)
End Sub
how can i do it in PowerBuilder

Code the following in the ÌtemChanged event of your detail datawindow (of course the code must be adapted to your specific situation):
decimal ld_qty, ld_unitprice
if Upper(dwo.name) <> 'ITEM' then
return
end if
select qty, unit_price into :ld_qty, :ld_unitprice from your_table where your_table.key = data;
dw2.SetItem(row, 'your_dw_col_qty', ld_qty)
dw2.SetItem(row, 'your_dw_unit_price', ld_unitprice)

If you have included the quantity and price with the data retrieved as part of the datawindow object you are using for the DDDW you can access it without another database call. Something like this (in itemchanged event):
datawindowchild dwc
string ls_name
long ll_row
IF Upper(dwo.Name) = "ITEM" THEN
GetChild("item", dwc)
ls_name = data
dwc.SetFilter("name = '" + ls_name +"'" )
dwc.Filter( ) // filter to specific row in dddw
ll_row = dwc.RowCount( ) // How many? Use the last one.
if ll_row > 0 then
SetItem(row, "cost", GetItemDecimal('cost', ll_row))
SetItem(row, "amount", GetItemDecimal('amount', ll_row))
end if
end if

Related

Powerbuilder : How to Get the RowNumber of specific data on datawindow

Good Day!,
Can anyone here give me a hint or idea on how to get the rownumber of specific value in datawindow.
i have 2 objects, treeview and datawindow with same Value. Treeview data are sorted but in datawindow is not. when i click an item in Treeview it will show the rownumber in datawindow correspond to value clicked in treeview. How can i do that?
Best regards,
Newnbie
P.S Sorry for my English
I would use dw.Find(*). Code example:
long ll_row
string ls_keyValue, ls_dwSearch
// Read value from TreeView
ls_keyValue = '...'
// "MyKey" :: Column in DataWindow containing the key read from tree view
ls_dwSearch = "MyKey = '" + ls_keyValue + "'"
ll_row = dw_data.Find(ls_dwSearch, 1, dw_data.RowCount( ))
if ll_row > 0 then
// Key value found => Make row current and visible
dw_data.ScrollToRow(ll_row)
end if

How to recursively query for many to many relationships in SQL Server 2012

I have a table in SQL 2012 that has a many to many relationship with itself through a junction table. It's laid out as follows
The many to many relationship I'm referring to is Currency_Dependency.
What I would like to do is make a recursive query that displays all dependencies in a tree fashion, so that each relationship is denoted with a number that shows how many steps away from the primary currency item being selected, as well as a number identifying which branch it's on. I would want, if possible, this query to go both ways, showing all of the currency items it depends on (Parent currency items), as well as all of the currency items that depend on it (child currency items). I have a query that shows only the first level both ways.
SELECT curr.Model AS 'Root Currency Item', cur2.Model AS 'Child Currency Item', cur3.Model AS 'Parent Currency Item'
FROM dbo.Currency curr
FULL JOIN dbo.Currency_Dependency cdep
ON curr.CurrencyId = cdep.CurrencyId
FULL JOIN dbo.Currency cur2
ON cdep.DependencyId = cur2.CurrencyId
FULL JOIN dbo.Currency_Dependency cdep2
ON curr.CurrencyId = cdep2.DependencyId
FULL JOIN dbo.Currency cur3
ON cdep2.CurrencyId = cur3.CurrencyId
WHERE curr.Status = 1 AND NOT(cur2.Model IS null AND cur3.Model IS null)
Which produces these results
So I would like additional columns for parent and child dependencies with that number denoting how many steps from the root currency item it is. I hope that makes sense.
Is such a query possible? I'm aware that a common table expression is something that is used for recursive queries but everything I've read on it so far has been pretty Greek (I'm a 2nd year Computer programming student)
Please let me know if you can help! Thanks so much!
Without example data I can't be sure, but I think this is what you are after:
;with cte as (
-- anchor elements: where curr.Status = 1 and not a dependent
select
CurrencyId
, Model
, ParentId = null
, ParentModel = convert(varchar(128),'')
, Root = curr.Model
, [Level] = convert(int,0)
, [Path] = convert(varchar(512),Model)
from dbo.Currency as curr
where curr.Status = 1
/* anchors do not depend on any other currency */
and not exists (
select 1
from dbo.Currency_Dependency i
where curr.CurrencyId = i.DependencyId
)
-- recursion begins here
union all
select
CurrencyId = c.CurrencyId
, Model = c.Model
, ParentId = p.CurrencyId
, ParentModel = convert(varchar(128),p.Model)
, Root = p.Root
, [Level] = p.[Level] + 1
, [Path] = convert(varchar(512),p.[Path] + ' > ' + c.Model)
from dbo.Currency as c
inner join dbo.Currency_Dependency as dep
on c.CurrencyId = dep.DependencyId
inner join cte as p
on dep.CurrencyId = p.CurrencyId
)
select * from cte

How to extract unique records(rows) from a table in MS ACCESS and save them to different tables

I am working in a project in MS ACCESS 2010 where i have a existing tables lets assume employee details and i have to extract each row based on the field first_name of employee and then make a different table for each of them.For example if first name is John then i should get all the records from employee details table where first name field is John and save all these rows in a new table i.e. new table_John and so on.
For this i have created a new table with only unique records of first name field and then running the below code, but i am getting an run time error 3067 : Query input must contain at least one table or query
Private Sub cmdRemoveDuplicates_Click()
Dim db As Database
Dim rst As Recordset
Dim rst1 As Recordset
Dim tbl As Table
Set db = CurrentDb
Set rst = db.OpenRecordset("tblDetails", dbOpenDynaset, dbSeeChanges)
'Make new table with unique records from first name field
CurrentDb.Execute "SELECT distinct first_name INTO [unique_records] from tblDetails;"
Set rst1 = db.OpenRecordset("unique_records", dbOpenDynaset, dbSeeChanges)
rst1.Requery
Do Until rst1.EOF
CurrentDb.Execute "SELECT tblDetails.* INTO tblNew" & rst1!first_name & _
" FROM unique_records LEFT JOIN tblDetails ON unique_records.first_name = tblDetails.first_name" & _
" WHERE (((tblDetails.first_name)='" & rst1!first_name & "'));"
rst1.MoveNext
Loop
rst1.Close
rst.Close
db.Close
Set rst1 = Nothing
Set rst = Nothing
Set db = Nothing
End Sub
My guess is you are getting a space or invalid character in there. Add [] around the table name, and if you don't want leading/training spaces, then TRIM them out.
CurrentDb.Execute "SELECT tblDetails.* INTO [tblNew" & rst1!first_name & _
"] FROM unique_records LEFT JOIN tblDetails ON unique_records.first_name = tblDetails.first_name" & _
" WHERE (((tblDetails.first_name)='" & rst1!first_name & "'));"
If you still get an error, assign the string in the EXECUTE to a string variable, and paste it into MS Access and see what it says. Sometimes the problem is obvious when looking at the actual SQL.

Use Probability for a field in a database is in the set (6, 12, 24)

I want to find all the fields in a database where 80% or more of the values are in the set (6,12,24).
Can I use sys.tables to do this with sys.columns to build a join to then look inside of each field for the probabilty?
Example for were this might apply is in the following list:
6 - Half a Dozen
12 - A Dozen
24 - Two Dozen
So the table would look something like this in procedural language:
select all the tables names in sys.tables and look in each number column in sys.columns for that table where 80% of the values are in the set (6,12,24).
Cribbing from my approach on this answer
https://stackoverflow.com/questions/20156747/number-of-null-values-for-every-column-on-ssis/20156981#20156981
You're going to need to look at a similar approach. Here I am using the system metadata to discover what columns are whole numbers (if you need to account for floating point numbers, factor that into the first filter).
SET NOCOUNT ON;
DECLARE
-- actual query
#query nvarchar(max)
-- templated query
, #template nvarchar(max);
-- Return column & schema/table combo anywhere
-- there is data in the column that starts like the
-- search key.
SELECT
#template = N'
SELECT COUNT(1) AS rc
, ''<COLUMN_NAME/>'' AS cname
, ''<SCHEMA/>.<TABLE_NAME/>'' AS tname
FROM
<SCHEMA/>.<TABLE_NAME/> T
WHERE
T.<COLUMN_NAME/> IN (6, 12, 24)';
DECLARE
CSR CURSOR
FOR
-- Iterate through all the columns
SELECT
ISC.TABLE_SCHEMA
, ISC.TABLE_NAME
, ISC.COLUMN_NAME
, ISC.IS_NULLABLE
FROM
INFORMATION_SCHEMA.COLUMNS ISC
WHERE
(
ISC.DATA_TYPE IN ('bigint', 'int', 'smallint', 'tinyint')
);
-- Cursor variables for capturing candidate schemas, tables and columns
DECLARE
#table_schema sysname
, #table_name sysname
, #column_name sysname
, #nullable varchar(3);
DECLARE
#RESULTS TABLE
(
instance_count bigint NOT NULL
, column_name sysname NOT NULL
, table_schema nvarchar(500) NOT NULL
);
OPEN
CSR;
FETCH NEXT
FROM
CSR
INTO
#table_schema
, #table_name
, #column_name
, #nullable;
WHILE (##FETCH_STATUS = 0)
BEGIN
-- stub in actual names, make 'em safe via quotename function
SET #query = REPLACE(#template, '<SCHEMA/>', quotename(#table_schema));
SET #query = REPLACE(#query, '<TABLE_NAME/>', quotename(#table_name));
SET #query = REPLACE(#query, '<COLUMN_NAME/>', quotename(#column_name));
BEGIN TRY
-- Dump results into a table variable
INSERT INTO
#RESULTS
EXECUTE(#query);
END TRY
BEGIN CATCH
-- print failing query
PRINT #query;
END CATCH
FETCH NEXT
FROM
CSR
INTO
#table_schema
, #table_name
, #column_name
, #nullable;
END
CLOSE CSR;
DEALLOCATE CSR;
WITH ROW_COUNTS AS
(
SELECT
s.[Name] as table_schema
, t.[name] as table_name
, SUM(p.rows) as TotalRowCount
FROM
sys.schemas s
LEFT OUTER JOIN
sys.tables t
ON s.schema_id = t.schema_id
LEFT OUTER JOIN
sys.partitions p
ON t.object_id = p.object_id
LEFT OUTER JOIN
sys.allocation_units a
ON p.partition_id = a.container_id
WHERE
p.index_id in(0,1) -- 0 heap table , 1 table with clustered index
AND p.rows is not null
AND a.type = 1 -- row-data only , not LOB
GROUP BY
s.[Name]
, t.[name]
)
SELECT
RC.table_schema
, RC.table_name
, R.column_name
, R.instance_count
, RC.TotalRowCount
-- ensure we don't divide by zero and perform floating division
, CAST(R.instance_count / (NULLIF(RC.TotalRowCount, 0) * 1.0) AS decimal(18,2)) AS InstancePercentage
FROM
ROW_COUNTS AS RC
INNER JOIN
#results R
ON R.table_schema = quotename(RC.table_schema) + '.' + quotename(table_name)
WHERE
CAST(R.instance_count / (NULLIF(RC.TotalRowCount, 0) * 1.0) AS decimal(18,2)) > .8;
If you drop the filter, then you'll see the percent of values found. It currently NULLs out the percentage for tables with zero rows.

Creating a table filled with values that are derived from a “super”-value

I'm making a Stored Procedure that receives a #UserId(int) and a #CategoryId(int).
I now have to sever any connection between the user, and the category, however - The user is only directly connected to one or more Subcategories (This connection is found in a table called "HelpWith"), that in turn are connected to a single category each (One category can contain multiple subcategories).
To find the values I have made the following SELECT-statement:
SELECT SubcategoryId
FROM [Subcategory] as s
LEFT JOIN [Category] as c ON c.CategoryId = s.CategoryId
WHERE c.Categoryid = #CategoryId
The SELECT-statement above can "find" multiple SubcategoryIds and thus, I figured I would have to somehow INSERT the value(s) into a table so that I could do something like:
DELETE FROM HelpWith
WHERE SubcategoryId IN (SELECT ID FROM #TempTable)
AND userId = #UserId
Does anyone know how this might be achieved?
Solved by using a JOIN in a DELETE
USE [MAPP]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[MA_SetTutor_RemoveCategory]
#UserId int,
#CategoryId int
AS
BEGIN
DELETE FROM HelpWith
WHERE SubcategoryId IN
(
SELECT SubcategoryId
FROM [Subcategory] AS s
LEFT JOIN [Category] AS c ON c.CategoryId = s.CategoryId
WHERE c.CategoryId = #CategoryId
AND userId = #UserId
)
END

Resources