Make Procedure To Check Balance &> Or &< Totalcost
Sep 9, 2007
helo all...,
i have create procedure can decrease totalcost from order table(database:games.dbo) with balance in bill table(database:bank.dbo). my 2 database in same server is name "boy"
i have 2 database like: bank.dbo and games.dbo
in games.dbo, have a table name is order(user_id,no_order,date,totalcost)
in bank.dbo, have a table name like is bill(no_bill,balance)
this is a list of bill table
no_bill balance
111222 200$
222444 10$
this is a list of order table
user_id no_order date totalcost
a 1 1/1/07 50$
when customer insert no_bill(111222) in page and click a button, then bill table became
no_bill balance
111222 150$
222444 10$
when customer insert no_bill(222444) in page and click a button, then message "sorry, your balance is not enough"
mystore procedure like:
ALTER PROCEDURE [dbo].[pay]
(
@no_bill AS INT,
@no_order AS int,
@totalcost AS money
)
AS
BEGIN
BEGIN TRANSACTION
DECLARE @balanc AS money
SET @balanc= (SELECT [balance] FROM Bank.dbo.bill WHERE [no_bill] = @no_bill)
UPDATE [bank.dbo.bill]
SET
[balance] = @balanc - @totalcost
WHERE
[no_bill] = @no_bill
COMMIT TRANSACTION
END
it can decrease money in bank, but i want it ceck money if balance > totalcost, so balance-totalcost,
if balance<totalcost,so error message"sorry, your balance not enough"
is it can make in procedure?
thx...
I have a table with Million plus records. I have been able to calculate the Trial_Balance for all months.
Now I am trying to provide a Beginning Balance for all months and the Logic is the Beginning Balance of July would be the Trial_Balance of June. I thought I could just do a self Join but this is not working.
UPDATE dbo.TrialBalance SET Beginning_Balance_Debit = B.Trial_Balance_Debit FROM (SELECT DATEADD(month, -1, Calendar_Month) AS PrevCalMonth,Trial_Balance_Debit FROM dbo.TrialBalance) AS B INNER JOIN dbo.TrialBalance A ON b.PrevCalMonth=A.Calendar_Month
How can I check to make sure that my dumps are not corrupted? I have been using a utility from Microsoft called DSCAN5, but have found that this has some limitations.
We have a database called Itemphotos. In this table is a field called 'PID' picture ID. We have a photos directory with files like 2181.jpg, 2182.jpg, 2184.jpg. The number is the ID number for the picture of the part. If a record exist in the database based on the PID "Primary Key" , check the directory to make sure the jpg file exist. Here is the code I have so far.
use [ItemPhotos] select pid from dbo.itemdata declare @file_path nvarchar(500) declare @file_exists int set @file_path = 'av-sql2c$inetpubphotosmacolaphotos' + [itemdata].[pid]
I have a table where i have to make a check constraint that states the first 3 characters of the customerid field must be the first 3 characters of the company name I am so lost I looked everywhere.
helo alll...,this is my data:my table is item(productid,stock) ,order(customerid,no_order), and orderdetail (no_order,productid,quantity) example: order and orderdetail displayed in gridview....order is displayed like this: customerid no_orderdetail A 1detail B 2 when i click detail in row 2, it's display orderdetail:no_order productid quantity 2 c1 2 2 p1 3 i have make all this is ok. but i want to decrease stok in item with quantity in orderdetail.my code in procedure like:CREATE PROCEDURE [dbo].[order_item](@productid AS varchar,@quantity AS INT)ASBEGINBEGIN TRANSACTIONDECLARE @no_order AS INTDECLARE @stock AS INTINSERT INTO [Orderdetail]([ProductId],[Quantity])VALUES(@productId,@quantity)SET @no_order = SCOPE_IDENTITY()SET @Stock = (SELECT [Stock] FROM [item] WHERE [ProductId] = @productId)UPDATE [item]SET[Stock] = @Stock - @quantityWHERE[ProductId] = @productIdCOMMIT TRANSACTIONENDreturn it can't work..how about his true code in store procedure?ok..., thx..
Can DTS make a call to a stored procedure on an AS/400 and accept data from that call. I need to access the AS/400 through OLE/DB for AS/400, execute the call to a stored procedure (the AS/400 stored procedure gets the data from DB2/400, executes some business logic, then presents the record set), and grab the record set returned and dump it into a SQL 7.0 table.
I have a bulk insert script. I have a stored procedure. I saved the stored procedure as a .sql file in another folder too. I have another program that can "run external program" and the only files it will run is a .bat or .exe. I want my other program to be able to trigger the stored procedure to run. I think this means I need a .bat file.
Here is my stored procedure:
USE [EricaTraining] GO /****** Object: StoredProcedure [dbo].[LoadDailyAdjReport] Script Date: 03/29/2013 10:56:42 ******/ SET ANSI_NULLS ON GO
I have created a stored procedure that will read the content of the text files of a particular folder. I need to make the stored procedure to run daily so that it will read the new files that is present in that folder. I have written a stored procedure to make the process of reading the file. But i need to know how to make the stored procedure to run daily so that it will automatically read all the files. I have got the information that it can be made possible using dts package. As i dont have any knowledge about dts package can anyone help me how to make this possible.
Can anyone point me to the right direction with the stored procedure on making a backup of the database. I am not looking for a scheduled backup. I'm looking for when the stored procedure get executed, the backup start right away. I believe it also require a username and password as well.
Basically, I'm working on a stored procedure which will retrieve data based on study parameter passed. The datasource is 'Views'. The name of the view is same for every study except that there is corresponding study name included. For example the views names are something like this for study abc 'v_abc_form' and for study def 'v_def_form'.
Below is the select statement I'm trying to use by declaring @study variable but not able to succeed. I'm not sure how to make the table name dynamic.
I need to quickly make this proc compatible with SQL 2005 and am struggling. I have alot of catching up to do.
Basically, it checks for Foreign Key dependencies in a database. There might be a better way to do this in SQL 2005 but for know I really need to get this working. Any help is verry much appreciated!
ALTER Procedure aes.Check_Dependent_Rows_Exist (@RowID int, @has_rows int OUTPUT ) AS BEGIN DECLARE @Colname varchar(200), @Tablename varchar(200) DECLARE @cnt int DECLARE @temp_row int DECLARE @owner varchar(25) DECLARE @ownerid int DECLARE @lstrSql nvarchar(2000) -- #1: declare cursor for maximum performance DECLARE lcur CURSOR LOCAL FORWARD_ONLY KEYSET READ_ONLY FOR
SELECT syscolumns.Name, OBJECT_NAME(fkeyid) AS FkeyTableName FROM sysreferences INNER JOIN syscolumns ON sysreferences.fkeyid=syscolumns.id AND fkey1=syscolumns.colid WHERE OBJECT_NAME(rkeyid)= 'customer'
OPEN lcur CREATE TABLE #Temp (DependentRows int) -- #2: only return a bit indicating if dependant rows exist or not
SET @has_rows = 0
FETCH NEXT FROM lcur INTO @Colname,@Tablename
WHILE @@FETCH_STATUS = 0 BEGIN SET @temp_row = 0
SELECT @ownerid = uid from sysobjects where name = @Tablename SELECT @owner = [name] from sysusers where uid = @ownerid
SET @lstrSql= 'insert into #Temp Select DependentRows = Count(' + @Colname + ') from ' + @owner + '.' + @TableName + ' where ' + @Colname + ' =' + CAST(@RowID AS VARCHAR(16)) + '' --print @lstrSql EXEC (@lstrSql) SELECT @temp_row = ISNULL(DependentRows,0) FROM #Temp IF @temp_row > 0 BEGIN -- #3: stop processing as soon as dependant rows are found to exist SET @has_rows = 1 BREAK END
FETCH NEXT FROM lcur INTO @Colname,@TableName
END deallocate lcur END GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO
error Server: Msg 208, Level 16, State 1, Line 1 Invalid object name 'dbo.order_detail'. Server: Msg 208, Level 16, State 1, Line 1 Invalid object name 'dbo.invoice_header'. Server: Msg 208, Level 16, State 1, Line 1 Invalid object name 'dbo.order_header'. Server: Msg 208, Level 16, State 1, Line 1 Invalid object name 'dbo.payment'. Server: Msg 208, Level 16, State 1, Line 1 Invalid object name 'dbo.cash_on_account'.
(1 row(s) affected)
Cannot grant, deny, or revoke permissions to sa, dbo, information_schema, sys, or yourself.
Hi ~ I made simple stored procedure that is to update user information following as... ALTER PROCEDURE UpdateUserProfile( @user_id uniqueidentifier, @user_firstname nvarchar(50), @user_lastname nvarchar(50), @user_birth nvarchar(20), @user_gender nvarchar(20) ) AS UPDATE user_profile SET user_firstname = @user_firstname, user_lastname = @user_lastname, user_birth = @user_birth, user_gender = @user_gender WHERE user_id = @user_id RETURN When I tried to save this procedure, I faced on "Invalid Object : UpdateUserProfile" error message. What's the problem ?
Hallo !I have a Table with a column "ordernumber"ordernumberA12A45A77A88Is it possible to create a stored procedure which makes a string of these column ?Result: string = ('A12','A45','A77','A88')Thanks !aaapaul
Below is the stored procedure i have it works fine if i have 1 value passed to @invited_by but i want to modify but i want this code to be working for multiple inputs .Lets say if i do
exec [dbo].[sp_GetInvitationStatusTest] 'Test1 . I get the desired output but i want this procedure to work for exec [dbo].[sp_GetInvitationStatusTest] 'Test1,Test2'. USE [merck_acronyms] GO
I would like SQL Server 2000 to distinguish between uppercase and lowercase letters, but only within a single stored procedure. Also, at the end of the sp, I want the original collation to be restored. How will I implement this in my sp?
Hey Guys, I hope someone can help on here with this. I have this Database where techs are scheduled and dispatched to perform tasks based on skus. What I am trying to achieve is finding the first available Tech based on their schedule and the appointments table. Example User enters today's date and 5:30 AM and the search for all available techs to perform that task
the tables ddl is USE [Schedule] GO /****** Object: Table [dbo].[AllDays] Script Date: 05/31/2006 01:13:49 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[AllDays]( [ID] [int] NOT NULL, [DayString] [varchar](20) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL, CONSTRAINT [PK_WorkingDays] PRIMARY KEY CLUSTERED ( [ID] ASC )WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY] ) ON [PRIMARY]
GO SET ANSI_PADDING OFF USE [Schedule] GO /***Appointments Table where trouble is ***** /****** Object: Table [dbo].[Appointments] Script Date: 05/31/2006 01:17:49 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO CREATE TABLE [dbo].[Appointments]( [ID] [int] IDENTITY(1,1) NOT NULL, [Customer_ID] [int] NOT NULL, [Tech_ID] [int] NOT NULL, [StartTime] [datetime] NOT NULL, [EndTime] [datetime] NOT NULL, [App_Date] [datetime] NOT NULL, [Created_By] [int] NOT NULL, [Date_Created] [datetime] NOT NULL, [Sku_ID] [int] NOT NULL, [Comment_ID] [int] NOT NULL, CONSTRAINT [PK_TechsShifts] PRIMARY KEY CLUSTERED ( [ID] ASC )WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY] ) ON [PRIMARY]
GO USE [Schedule] GO ALTER TABLE [dbo].[Appointments] WITH CHECK ADD CONSTRAINT [FK_Appointments_Comments] FOREIGN KEY([Comment_ID]) REFERENCES [dbo].[Comments] ([ID]) GO ALTER TABLE [dbo].[Appointments] WITH CHECK ADD CONSTRAINT [FK_Appointments_Techs] FOREIGN KEY([Tech_ID]) REFERENCES [dbo].[Techs] ([ID])
USE [Schedule] GO /****** Object: Table [dbo].[Schedule] Script Date: 05/31/2006 01:19:31 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO CREATE TABLE [dbo].[Schedule]( [ID] [int] IDENTITY(1,1) NOT NULL, [Tech_ID] [int] NOT NULL, [AllDayID] [int] NOT NULL, [ShiftStartTime] [datetime] NOT NULL, [ShiftEndTime] [datetime] NOT NULL, CONSTRAINT [PK_Schedule] PRIMARY KEY CLUSTERED ( [ID] ASC )WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY] ) ON [PRIMARY]
GO USE [Schedule] GO ALTER TABLE [dbo].[Schedule] WITH CHECK ADD CONSTRAINT [FK_Schedule_AllDay] FOREIGN KEY([AllDayID]) REFERENCES [dbo].[AllDays] ([ID])
Plus the Techs Table which holds their ID's names etc I have a snapshot (http://webdivisions.net/images/relation.gif) of the relationship posted here if that can help
I'm using some security scanning software which looks for vulnerabilities in the database. It tells me that some stored procedure are should not be given public permission. How do i know whether the stored procedures are being used by someone or last used on which date? Is there any way to find out?
At the same time, how do i check the permission of stored procedure on ms sql 2005? Thanks
I have some stored procedures that run on SQL2K(SP3) /WIN2K. Sometimes I modify them using ALTER PROCEDURE statements. How to check if they are changed after ALTER statements run? Thanks.
I have a long running procedure (batch job) which will take quite some time to complete. I need to run a fixed number (configurable value) of copies simultaneously, i.e., either two copies or three copies at a time. (I don't want to use Service Broker for this purporse now)
For this I need to know whether this procedure is currenly being executed, and how many copies. Now I'm maintaing a flag in a table where this will get updated once the procedure starts and ends. This is not 100% reliable and need a manual check occasionally.
I would like to know, whether there is an easier method to find whether this procedure is being executed currently ?
I have a store Procedure modify structTable but check syntax is error !
Please help me ? -------------------------------------------------------------------------------------------------------------------- IF EXISTS(SELECT NAME FROM SYSOBJECTS WHERE NAME ='MODISTRCTTABLE ' AND TYPE='P') BEGIN DROP PROCEDURE MODISTRUCT_TABLE END GO
CREATE PROCEDURE MODISTRUCT_TABLE @TABLE_NAME VARCHAR(60), @COLUMN_NAME VARCHAR(60), @COLUMN_TYPE VARCHAR(60), @COLUMN_SIZE INT(10) AS BEGIN IF EXISTS (SELECT @COLUMN_NAME FROM syscolumns) BEGIN ALTER TABLE @TABLE_NAME ALTER @COLUMN_NAME + ' ' + @COLUMN_TYPE+'('+ @COLUMN_SIZE +')' END ELSE BEGIN ALTER TABLE @TABLE_NAME ADD @COLUMN_NAME + ' ' + @COLUMN_TYPE+'('+ @COLUMN_SIZE +')' END END
I am trying to write a trigger which updates the customers balance when an invoice is deleted. The tables are created and I can post if needed, but I get a compilation error when I use the follow code.
create or replace trigger trg_updatecustbalance2 after delete on invoice for each row begin update customer set cust_balance = cust_balance - inv_amount where customer.cust_num = invoice.cust_num; end; /
dear all experts please kindly provide ur help thanks.
suppose i hv 2 records in a table as follow:
customer_id status qty date
123456 install 24 2008-02-20
123456 disconnect -24 2008-02-20
how can i exclude these 2 records from my table? actually you can see the net gain is 0, but in my report reflects install qty is 24 and disconnect qty is -24. i dont want these 2 records reflect in my report, how can i achieve such result? thanks in advance!
I started to use data mining sql server recently, and I have a little problem:
I'm trying to predict a boolean variable, but my database has only 1/4 of true results, because of that my predict probability is a little bit low in this kind of cases.
My question is: Is there a way of increasing the importance of a database entry in order to avoid editing a database with 40000 entries?
Now Sql Function can not modify data as Sql Procedure like other database, it's very troublesome at most case! in most case, Sql Function is used to improve code structure then it can be maintanced easy! BUT only because it can not modify data, there are 2 troublesome way:
1. Make all callers in the path from Sql Functions to Sql Procedure. and the coder will cry, the code will become very confusional , and very difficult to maintance.
2. Divide the Sql Function into a thin CLR wrapper to call a Sql Procedure, can only use another connection, BUT can not be in the same transaction context And the code is ugly and slow.
You should not give limitation to Sql Function, should just limit the using context of Sql Function!
The sql code is more difficult to read and maintance than norm code(C#), then the improving code structure and readability should be one of your most important task, that's why microsoft!
I've a stored procedure which retrieves based on different criterias. I added one more critieria - to display a column based on a range of values. The values are @OriginalMin and @OriginalMax. I declared the variables and gave the conditions. But still set ANSI_NULLS ON set QUOTED_IDENTIFIER OFF GO ALTER procedure [dbo].[USP_Account_Search_Mod]@OriginalMin DECIMAL=0 ,@OriginalMax DECIMAL=0 AS DECLARE @CRI8 VARCHAR(500)SELECT @CRI1='' SET @CRI8='AND OriginalBalance >=@OriginalMin AND OriginalBalance<=@OriginalMax' SELECT @Criteria = ......+ @CRI8 When I execute this stored procedure, I get the following error message. SELECT * FROM dbo.UDV_Tier1Accounts WHERE CUSTOMER IN (SELECT CUSTOMERNUMBER FROM dbo.UDF_GetUsersCustomers(3)) AND Customer = '00001'AND OriginalBalance >=@OriginalMin AND OriginalBalance<=@OriginalMax UNION SELECT * FROM dbo.UDV_Tier2Accounts WHERE CUSTOMER IN (SELECT CUSTOMERNUMBER FROM dbo.UDF_GetUsersCustomers(3)) AND Customer = '00001'AND OriginalBalance >=@OriginalMin AND OriginalBalance<=@OriginalMaxORDER BY NAME ASC Msg 137, Level 15, State 2, Line 1 Must declare the variable '@OriginalMin'. Msg 137, Level 15, State 2, Line 1 Must declare the variable '@OriginalMin Could someone tell what's wrong with the procedure? For convenience, I've included only the latest critieria I added.
I have a Stored Procedure as followsUSE [MyDataBase] GO /****** Object: StoredProcedure [dbo].[SPLogins] Script Date: 05/24/2008 21:58:50 ******/ SET ANSI_NULLS OFF GO SET QUOTED_IDENTIFIER ON GO Create PROCEDURE [dbo].[SPLogins] ( @LoginName varchar(50), @LoginD int output, ) AS Select @LoginID = LoginID From MyDatabase Where (LoginName Is not Null) and @LoginName = LoginName
I am trying to check for a null value in the Stored Procedure. I dont' get an error, but it doesn't catch the "null" and gives an error when there is a null value What Is the correct wayt to go about it.