Selecting parameter from values of a SQL Query - sql

I need to produce a report that selects a set of values from a database table based on a company. I would like the user to be able to select the name of the company from a list of available companies. The companies have 2 associated unique database ID code number. Based on what companies the user selects I need the sql query to pass to the parameter both unique codes to the parameter.
So, in short, how do I create a sql query that would show the company names and when selecting the company would then select both unique codes based on the company name I select from a single select drop down. Use the value selected from that drop-down list to run the SQL query in the report itself?
Thanks for any help or advice you can offer!

Pass the company name to to your stored proc instead of the two unique codes, then find the codes for the company inside your procedure.
CREATE someProc (#Company VARCHAR(100))
AS BEGIN
DECLARE #ID1 INT, #ID2 INT
SELECT #ID1 = someID1, #ID2 = someID2
FROM someDatabase
WHERE companyName=#Company

Related

How to add updated information to one table to another with previous information

I have two tables first one master's table second one customers table.in customers table updated information will be stored ,how to add updated information to the first table including previous information.so how to add updated information to the master's table.
you could create a stored procedure to execute both the queries like this. Syntax might be incorrect so check for your self and google about stored procedures.
create procedure updatecustomer
#customerid int,
#customercolumn data_type
as
update customer set customercolumn = #customercolumn where customerid = #customerid
insert into admin(customerid, customercolumn) values(#customerid, #customercolumn)

Stored Procedure to display values from sql server

I am developing a windows application in vb.net. I have a requirement to retrieve values from a table based on certain criteria. My sql server table is as below.
tbl ACCOUNT_DETAIL
I have a textbox in my windows form for displaying the corresponding GL_Account. The user will pass the paidto value to the back end and the procedure must return the corresponding GL_Account. But there is one more criteria : while retrieving the GL_Account from the database, we need to check for wholefamily. If the WholeFamily is 1 then all the Accounts(should be fetched from another table) which starts with the GL_Account should be retrieved. Else only the GL_Account from "this" table needs to be sent. The table which contains the entire account information is below
tbl ACCOUNT_MASTER.
I have shown only a part of the data from the table here.
I have created a Cursor to fetch values from the Account_Detail table. But after fetching values i need to check for WholeFamily and based on the bit i need to pass values to the front end.
#PaidTo(int) is the SP Parameter that is passed from the frontend.
DECLARE #GLAccount VARCHAR(50)
DECLARE #AccountName VARCHAR(MAX)
DECLARE PV_CURSOR CURSOR
FOR
SELECT GL_Account,WholeFamily
FROM ACCOUNT_DETAIL
WHERE PaidTo=#PaidTO
OPEN PV_CURSOR
FETCH NEXT FROM PV_CURSOR
INTO #GLAccount,#AccountName
Now I am stuck as to how to proceed further. Sorry for my ignorance.
Please help me.
I would not use a cursor in such a scenario...
If I understand what you are suggesting correctly, incase Whole_family is 1 then you need to pull all info from the other table in the same result set. The below query can have multiple rows for every GL_Account based on your entries in the Account_master.
SELECT PAIDTO
, GL_ACCOUNT
, WHOLEFAMILY
, ACCOUNTNUMBER
, ENGLISHNAME
FROM ACCOUNT_DETAIL
LEFT JOIN ACCOUNT_MASTER
ON CASE
WHEN WHOLEFAMILY = 1
THEN ACCOUNT_DETAIL.GL_ACCOUNT
ELSE NULL
END = ACCOUNT_MASTER.ACCOUNTNUMBER

Selecting data from more than 2000 databases?

How i can search / select a field in more than 2000 databases in sql server.
i have a main database consisting a table called 'Kewword" where i store key world under 'kewwordtitle' field in keyword table, when new user register a new database are created for the user and user use a keyword,
now the situtation is, how i can find that how much user use a key work, here keywordtitle is primary key,...
thanks/
Your question is a little bit fuzzy, but if this is a one shot and if all your databases are on the same instance, you could do something like:
declare #t table(col int)
insert #t
exec sp_MSforeachdb 'use ?; select 1 from keyword where keywordtitle = ''<yourkeyword>'''
select count(*) from #t

SQL: I need to take two fields I get as a result of a SELECT COUNT statement and populate a temp table with them

So I have a table which has a bunch of information and a bunch of records. But there will be one field in particular I care about, in this case #BegAttField# where only a subset of records have it populated. Many of them have the same value as one another as well.
What I need to do is get a count (minus 1) of all duplicates, then populate the first record in the bunch with that count value in a new field. I have another field I call BegProd that will match #BegAttField# for each "first" record.
I'm just stuck as to how to make this happen. I may have been on the right path, but who knows. The SELECT statement gets me two fields and as many records as their are unique #BegAttField#'s. But once I have them, I haven't been able to work with them.
Here's my whole set of code, trying to use a temporary table and SELECT INTO to try and populate it. (Note: the fields with # around the names are variables for this 3rd party app)
CREATE TABLE #temp (AttCount int, BegProd varchar(255))
SELECT COUNT(d.[#BegAttField#])-1 AS AttCount, d.[#BegAttField#] AS BegProd
INTO [#temp] FROM [Document] d
WHERE d.[#BegAttField#] IS NOT NULL GROUP BY [#BegAttField#]
UPDATE [Document] d SET d.[#NumAttach#] =
SELECT t.[AttCount] FROM [#temp] t INNER JOIN [Document] d1
WHERE t.[BegProd] = d1.[#BegAttField#]
DROP TABLE #temp
Unfortunately I'm running this script through a 3rd party database application that uses SQL as its back-end. So the errors I get are simply: "There is already an object named '#temp' in the database. Incorrect syntax near the keyword 'WHERE'. "
Comment out the CREATE TABLE statement. The SELECT INTO creates that #temp table.

Retrieve inserted row ID in SQL

How do I retrieve the ID of an inserted row in SQL?
Users Table:
Column | Type
--------|--------------------------------
ID | * Auto-incrementing primary key
Name |
Age |
Query Sample:
insert into users (Name, Age) values ('charuka',12)
In MySQL:
SELECT LAST_INSERT_ID();
In SQL Server:
SELECT SCOPE_IDENTITY();
In Oracle:
SELECT SEQNAME.CURRVAL FROM DUAL;
In PostgreSQL:
SELECT lastval();
(edited: lastval is any, currval requires a named sequence)
Note: lastval() returns the latest sequence value assigned by your session, independently of what is happening in other sessions.
In SQL Server, you can do (in addition to the other solutions already present):
INSERT INTO dbo.Users(Name, Age)
OUTPUT INSERTED.ID AS 'New User ID'
VALUES('charuka', 12)
The OUTPUT clause is very handy when doing inserts, updates, deletes, and you can return any of the columns - not just the auto-incremented ID column.
Read more about the OUTPUT clause in the SQL Server Books Online.
In Oracle and PostgreSQL you can do this:
INSERT INTO some_table (name, age)
VALUES
('charuka', 12)
RETURNING ID
When doing this through JDBC you can also do that in a cross-DBMS manner (without the need for RETURNING) by calling getGeneratedKeys() after running the INSERT
I had the same need and found this answer ..
This creates a record in the company table (comp), it the grabs the auto ID created on the company table and drops that into a Staff table (staff) so the 2 tables can be linked, MANY staff to ONE company. It works on my SQL 2008 DB, should work on SQL 2005 and above.
===========================
CREATE PROCEDURE [dbo].[InsertNewCompanyAndStaffDetails]
#comp_name varchar(55) = 'Big Company',
#comp_regno nchar(8) = '12345678',
#comp_email nvarchar(50) = 'no1#home.com',
#recID INT OUTPUT
-- The '#recID' is used to hold the Company auto generated ID number that we are about to grab
AS
Begin
SET NOCOUNT ON
DECLARE #tableVar TABLE (tempID INT)
-- The line above is used to create a tempory table to hold the auto generated ID number for later use. It has only one field 'tempID' and its type INT is the same as the '#recID'.
INSERT INTO comp(comp_name, comp_regno, comp_email)
OUTPUT inserted.comp_id INTO #tableVar
-- The 'OUTPUT inserted.' line above is used to grab data out of any field in the record it is creating right now. This data we want is the ID autonumber. So make sure it says the correct field name for your table, mine is 'comp_id'. This is then dropped into the tempory table we created earlier.
VALUES (#comp_name, #comp_regno, #comp_email)
SET #recID = (SELECT tempID FROM #tableVar)
-- The line above is used to search the tempory table we created earlier where the ID we need is saved. Since there is only one record in this tempory table, and only one field, it will only select the ID number you need and drop it into '#recID'. '#recID' now has the ID number you want and you can use it how you want like i have used it below.
INSERT INTO staff(Staff_comp_id)
VALUES (#recID)
End
-- So there you go. I was looking for something like this for ages, with this detailed break down, I hope this helps.